[英]Getting a setOnClickListener error when I try to run this app
我试图使一个按钮链接到我称为“视图视图”的View类,唯一的问题是应用程序崩溃并且在logcat中出现错误。 如果有人对这个问题有任何有用的信息,我将不胜感激。 这是我的Main.class代码
public class Main extends Activity {
DrawingView v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//LinearLayout layout1 = new LinearLayout (this);
//FrameLayout game = new FrameLayout(this);
//final DrawingView s = new DrawingView (this);
//TextView myText = new TextView(this);
Button startButton = (Button) findViewById(R.id.startbutton);
//redCircle.setWidth(300);
//redCircle.setText("niggers");
setContentView(R.layout.main);
//layout1.addView(myText);
//layout1.addView(redCircle);
//redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//game.addView(myText);
//game.addView(v);
//game.addView(layout1);
//setContentView(R.layout.main);
//setContentView(v);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Main.this, DrawingView.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是logcat错误
04-22 05:12:21.693: E/AndroidRuntime(1921): FATAL EXCEPTION: main
04-22 05:12:21.693: E/AndroidRuntime(1921): Process: com.Tripps.thesimplegame, PID: 1921
04-22 05:12:21.693: E/AndroidRuntime(1921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.os.Looper.loop(Looper.java:135)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-22 05:12:21.693: E/AndroidRuntime(1921): at java.lang.reflect.Method.invoke(Native Method)
04-22 05:12:21.693: E/AndroidRuntime(1921): at java.lang.reflect.Method.invoke(Method.java:372)
04-22 05:12:21.693: E/AndroidRuntime(1921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-22 05:12:21.693: E/AndroidRuntime(1921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-22 05:12:21.693: E/AndroidRuntime(1921): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
04-22 05:12:21.693: E/AndroidRuntime(1921): at com.Tripps.thesimplegame.Main.onCreate(Main.java:49)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.Activity.performCreate(Activity.java:5933)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-22 05:12:21.693: E/AndroidRuntime(1921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-22 05:12:21.693: E/AndroidRuntime(1921): ... 10 more
更改顺序,
Button startButton = (Button) findViewById(R.id.startbutton);
setContentView(R.layout.main);
至,
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);
确保在setContentView
指定了正确的布局,并检查按钮在布局中是否具有id startbutton
。
您忘记正确实施此行
setContentView(R.layout.main);
以这种方式实施
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);
地点
Button startButton = (Button) findViewById(R.id.startbutton);
后
setContentView(R.layout.main);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.