imageButton的空例外错误

时间:2016-05-10 20:29:20

标签: java android

我的imageButton出了问题。我已经查看了各种解决方案,它们要么不是相同的问题,要么就是没有用。

该按钮处于辅助活动中,在加载时会导致程序崩溃。

这是xml:

 <ImageButton
    android:id="@+id/back_arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:src="@drawable/back_arrow"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

这是java:

ImageButton imageButton;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.head_zoom);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    addListenerOnButton();
}

public void addListenerOnButton(){
    imageButton = (ImageButton) findViewById(R.id.back_arrow);
    imageButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.hairStyle = hairTypeArray.get(hairSpinner.getSelectedItemPosition());
            MainActivity.hairColor = hairColorArray.get(hairColor.getSelectedItemPosition());
            finish();
        }
    });
}

此处还有错误:

05-10 21:30:21.342 17254-17254/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: cameron.myapplication, PID: 17254
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{cameron.myapplication/cameron.myapplication.HeadZoom}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                   at cameron.myapplication.HeadZoom.addListenerOnButton(HeadZoom.java:83)
                                                   at cameron.myapplication.MainActivity.onCreate(MainActivity.java:40)
                                                   at cameron.myapplication.HeadZoom.onCreate(HeadZoom.java:39)
                                                   at android.app.Activity.performCreate(Activity.java:6251)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

0 个答案:

没有答案