我已经初始化了按钮,但它仍然说它是空的

时间:2018-06-13 14:36:03

标签: android performance android-layout nullpointerexception

NullPointerException应该出现,当我有一个空值,我调用我不知道如何调用普通按钮调用

6-13 19:22:10.359 19386-19386/com.example.saisriram.thevote E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.saisriram.thevote, PID: 19386
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.saisriram.thevote/com.example.saisriram.thevote.MainPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5265)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
 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.saisriram.thevote.MainPage.onCreate(MainPage.java:21)
    at android.app.Activity.performCreate(Activity.java:6033)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5265) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

这是日志显示的内容,我不明白为什么,这是我在Main中的代码:

package com.example.saisriram.thevote;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Handler;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.ImageButton;

 public class MainPage extends Activity {
 public static int NO_TIME = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button mainPage = (Button)findViewById(R.id.mainPage);

    mainPage.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run(){
                    Intent intent = new Intent(MainPage.this, HeadBoy.class);
                    startActivity(intent);
                    finish();
                }
            }, NO_TIME);
        }
    });
    ImageButton resultClick = (ImageButton) findViewById(R.id.resultClick);
    resultClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainPage.this, Results.class);
            startActivity(intent);
        }
    });

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main_page);


}
}

我搜索了那个错误,它说它发生在按钮没有被初始化但我确实初始化了我的,所以我没有弄错了。

1 个答案:

答案 0 :(得分:5)

在通过View实际设置活动内容之前,您正在使用findViewById(int)搜索setContentView(int)

setContentView(R.layout.activity_main_page);

之前致电Button mainPage = (Button)findViewById(R.id.mainPage);