ImageButton Null指针异常

时间:2014-07-16 12:06:29

标签: android

在完成ImageButton时,我在运行时收到空指针异常。

StackTrace:

 E/AndroidRuntime(1308): FATAL EXCEPTION: main
 E/AndroidRuntime(1308): Process: com.stephen.application, PID: 1308
 E/AndroidRuntime(1308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stephen.application/com.stephen.application.MainActivity}: java.lang.NullPointerException
 E/AndroidRuntime(1308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 E/AndroidRuntime(1308):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1308):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1308):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1308):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1308):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1308):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1308):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1308):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1308):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1308):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1308):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1308): Caused by: java.lang.NullPointerException
 E/AndroidRuntime(1308):    at com.stephen.application.MainActivity.onCreate(MainActivity.java:21)
 E/AndroidRuntime(1308):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(1308):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(1308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(1308):    ... 11 more

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {

    ImageButton imageButton1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         ImageButton ImageButton1=(ImageButton)findViewById(R.id.imageButton1);

        imageButton1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            Toast.makeText(MainActivity.this,"ImageButton is Clicked", Toast.LENGTH_SHORT).show();  

            }
        });

}

}  

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/android" />

</LinearLayout>

我不知道如何解决这些问题。任何人都可以帮我解决这些问题。谢谢。

4 个答案:

答案 0 :(得分:2)

无需创建 ImageButton 的其他对象。您可以使用在全局 ImageButton imageButton1; 中声明的内容。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageButton1=(ImageButton)findViewById(R.id.imageButton1);

        imageButton1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            Toast.makeText(MainActivity.this,"ImageButton is Clicked", Toast.LENGTH_SHORT).show();  

            }
        });

答案 1 :(得分:1)

您已为 ImageButton1 定义了值并致电 imageButton1

看到差异...... 这就是为什么它给你一个NullPointerException

所以更好的解决方案是你应该使用

ImageButton ImageButton1=(ImageButton)findViewById(R.id.imageButton1);
ImageButton1.setOnClickListener(new OnClickListener() { }

imageButton1=(ImageButton)findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new OnClickListener() { }

答案 2 :(得分:0)

这是问题

  ImageButton ImageButton1=(ImageButton)findViewById(R.id.imageButton1);

将其更改为imageButton1=(ImageButton)findViewById(R.id.imageButton1);

答案 3 :(得分:0)

在您的MainActivity.java中,行imageButton1.setOnClickListener(new OnClickListener() {应该与ImageButton1.setOnClickListener(new OnClickListener() {类似。

只是一个拼写错误.. :(