我试图在主屏幕上使用五个按钮构建应用程序。按下每个按钮时会弹出一个新的活动。无法启动活动(java.lang.IllegalStateException:已连接)
当我打第二个按钮会显示错误:
**java.lang.RuntimeException: Unable to start activity ComponentInfo{debuggers.os_project/debuggers.os_project.SJF}: java.lang.IllegalStateException: Already attached**
第一个按钮是工作的罚款。直到我只编码了两个按钮。
我main_activity.java是:
package debuggers.os_project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button fcfs;
private static Button sjf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fcfs = (Button)findViewById(R.id.fcfs);
sjf = (Button) findViewById(R.id.sjf);
fcfs.setOnClickListener(
new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FCFS.class);
startActivity(intent);
}
}
);
}
public void onSJF(View view){
Intent intent = new Intent(view.getContext(),SJF.class);
startActivity(intent);
}
}
为您的变量提供有意义的名称将极大地帮助您阅读代码... –
分享SJF活动的代码。我认为问题在于此。这可能有所帮助:http://stackoverflow.com/questions/35384888/java-lang-illegalstateexception-already-attached –
@PavanPatil你是对的,问题出在SJF的活动。谢谢你的帮助。 –