在我的Android应用程序中,我有两个活动类。我在第一个按钮上有一个按钮,我想在点击它时显示第二个按钮,但是我收到错误。以下是课程:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button)findViewById(R.id.startBtn);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, StartField.class);
startActivity(intent);
}
});
}
}
这是第二节课:
public class StartField extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
Button startButton = (Button) findViewById(R.id.end);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
我在Manifest文件中添加了第二个类。当我按下那个按钮时,出现错误,在logcat中显示
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.tavatievs.kurstoti.StartField.onCreate(StartField.java:20)