尝试运行此应用程序时出现setOnClickListener错误

时间:2015-04-22 05:20:28

标签: java javascript android jquery button

我正在尝试创建一个按钮链接到我所做的名为绘图视图的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

3 个答案:

答案 0 :(得分:2)

更改订单
Button startButton = (Button) findViewById(R.id.startbutton);
setContentView(R.layout.main);

要,

setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);

确保在setContentView&中指定了正确的布局检查按钮是否在您的布局中标有startbutton

答案 1 :(得分:0)

您忘记正确执行此行

setContentView(R.layout.main);

以这种方式实施

setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);

答案 2 :(得分:0)

地方

 Button startButton = (Button) findViewById(R.id.startbutton);

     setContentView(R.layout.main);