按钮单击时出现空指针异常

时间:2014-07-31 15:25:48

标签: android button nullpointerexception onclicklistener

我有4个图像作为按钮,当选择了正确的按钮时,会出现一个正常工作的箭头按钮。

我的问题是我尝试更改每个按钮的后台资源,以便在单击此箭头时更改,但我在此行获取空指针异常:

happybutton.setBackgroundResource(R.drawable.mum1);

我在java onCreate中声明了nextArrow按钮 -

nextArrow = (Button) findViewById(R.id.nextArrow);
nextArrow.setOnClickListener(onClickListener);

类:

protected void onCreate (Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.expressions);

        Button happybutton = (Button) findViewById(R.id.happybutton);
        happybutton.setOnClickListener(onClickListener);
        Button sadbutton = (Button)findViewById(R.id.sadbutton);
        sadbutton.setOnClickListener(onClickListener);
        Button worriedbutton = (Button) findViewById(R.id.worriedbutton);
        worriedbutton.setOnClickListener(onClickListener);
        Button excitedbutton = (Button) findViewById(R.id.excitedbutton);
        excitedbutton.setOnClickListener(onClickListener);
        //Button nextArrow = (Button) findViewById(R.id.nextArrow);
        //nextArrow.setOnClickListener(onClickListener);

        tick = (ImageView) findViewById(R.id.tick);
        cross = (ImageView)findViewById(R.id.cross);
        cross2 = (ImageView) findViewById(R.id.cross2);
        cross3 = (ImageView) findViewById(R.id.cross3);

        nextArrow = (Button) findViewById(R.id.nextArrow);
        nextArrow.setOnClickListener(onClickListener);


        tryagain = MediaPlayer.create(Expressions.this,R.raw.tryagain);
        correct = MediaPlayer.create(Expressions.this,R.raw.correct);

    }

private OnClickListener onClickListener = new OnClickListener(){

    public void onClick(final View view){
        switch(view.getId()){

        case R.id.happybutton:

            tick.setVisibility(View.VISIBLE);
            nextArrow.setVisibility(View.VISIBLE);
            correct.start();
            break;

        case R.id.sadbutton:

            cross.setVisibility(View.VISIBLE);
            tryagain.start();
            break;

        case R.id.worriedbutton:

            cross3.setVisibility(View.VISIBLE);
            tryagain.start();
            break;

        case R.id.excitedbutton:

            cross2.setVisibility(View.VISIBLE);
            tryagain.start();
            break;


        case R.id.nextArrow:

            happybutton.setBackgroundResource(R.drawable.mum1);
            worriedbutton.setBackgroundResource(R.drawable.mum2);
            sadbutton.setBackgroundResource(R.drawable.mum3);
            excitedbutton.setBackgroundResource(R.drawable.mum4);
            break;
        }   
    }   

logcat的:

 07-31 16:17:11.312: E/AndroidRuntime(7818): FATAL EXCEPTION: main
    07-31 16:17:11.312: E/AndroidRuntime(7818): java.lang.NullPointerException
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at 
com.example.Autism_App.Expressions$1.onClick(Expressions.java:84)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.view.View.performClick(View.java:4383)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.view.View$PerformClick.run(View.java:18097)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.os.Handler.handleCallback(Handler.java:725)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.os.Looper.loop(Looper.java:176)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at android.app.ActivityThread.main(ActivityThread.java:5279)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    07-31 16:17:11.312: E/AndroidRuntime(7818):     at dalvik.system.NativeStart.main(Native Method)

的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/question"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="29dp"
        android:text="Which face is the happy face?"
        android:textSize="80sp" />

    <Button
        android:id="@+id/happybutton"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/excitedbutton"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="43dp"
        android:background="@drawable/happyface"/>

    <Button
        android:id="@+id/nextArrow"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="65dp"
        android:visibility="invisible"
        android:background="@drawable/nextarrow" />

    <Button
        android:id="@+id/worriedbutton"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignBaseline="@+id/happybutton"
        android:layout_alignBottom="@+id/happybutton"
        android:layout_alignLeft="@+id/sadbutton"
        android:background="@drawable/worriedface" />

    <Button
        android:id="@+id/sadbutton"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_below="@+id/question"
        android:layout_marginRight="113dp"
        android:layout_marginTop="29dp"
        android:layout_toLeftOf="@+id/nextArrow"
        android:background="@drawable/sadface" />

    <Button
        android:id="@+id/excitedbutton"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignBaseline="@+id/sadbutton"
        android:layout_alignBottom="@+id/sadbutton"
        android:layout_alignLeft="@+id/question"
        android:layout_marginLeft="116dp"
        android:background="@drawable/excitedface" />

    <ImageView
        android:id="@+id/cross2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/excitedbutton"
        android:layout_alignTop="@+id/excitedbutton"
        android:src="@drawable/cross"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/cross"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/sadbutton"
        android:layout_alignTop="@+id/sadbutton"
        android:src="@drawable/cross"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/tick"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/excitedbutton"
        android:layout_alignTop="@+id/worriedbutton"
        android:src="@drawable/tick"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/cross3"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/worriedbutton"
        android:layout_alignTop="@+id/worriedbutton"
        android:src="@drawable/cross"
        android:visibility="invisible" />

</RelativeLayout>

我错过了一些非常明显的东西吗?

3 个答案:

答案 0 :(得分:5)

您正在初始化本地变量而不是实例字段

protected void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.expressions);

    Button happybutton = (Button) findViewById(R.id.happybutton);
    happybutton.setOnClickListener(onClickListener);

当您说Button happybutton时,您正在创建一个 new 变量,其范围仅限于onCreate()方法,并且在此方法的持续时间内将取代该名称的实例字段。您初始化此变量,因此调用其setOnClickListener方法可以正常工作。但是变量在onCreate()方法的末尾消失,而其他方法只看到同名的从未初始化的实例字段。

相反,只需初始化您可能已在onCreate()之外声明的实例字段

Button happybutton;

protected void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.expressions);

    happybutton = (Button) findViewById(R.id.happybutton);
    happybutton.setOnClickListener(onClickListener);

这可能是一个相当微妙的错误,因为使用局部变量取代实例字段是完全有效的代码,不会触发任何编译器投诉。

答案 1 :(得分:0)

我相信,班级成员happybutton未初始化。

答案 2 :(得分:0)

按钮应该是类变量,如下所示:

public class MyClass {
    Button happybutton;
    Button sadbutton;
    Button worriedbutton;
    Button excitedbutton;

    protected void onCreate (Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.expressions);

        happybutton = (Button) findViewById(R.id.happybutton);
        happybutton.setOnClickListener(onClickListener);
        sadbutton = (Button)findViewById(R.id.sadbutton);
        sadbutton.setOnClickListener(onClickListener);
        worriedbutton = (Button) findViewById(R.id.worriedbutton);
        worriedbutton.setOnClickListener(onClickListener);
        excitedbutton = (Button) findViewById(R.id.excitedbutton);
        excitedbutton.setOnClickListener(onClickListener);
...