自定义OnClickListener类出错

时间:2014-03-28 22:14:21

标签: android nullpointerexception onclicklistener logcat

应用程序强制关闭甚至没有启动,请参阅下面的logcat信息和(当下面3行被注释掉时应用程序工作)

  • 在LogCat中也说NullPointerException存在问题,但我不知道在哪里以及如何解决它。

XML文件:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.figurehowtodo.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/produceText1"
    android:layout_width="280dp"
    android:layout_height="50dp"
    android:text="@string/hello_world"
    />

<Button
    android:id="@+id/myFirstButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ClickMe1" />

<Button
    android:id="@+id/mySecondButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ClickMe2" />

<Button
    android:id="@+id/myThirdButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ClickMe3" />

</LinearLayout>

MainActivity.java

package com.example.figurehowtodo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

Button myFirstButton;
Button mySecondButton;
Button myThirdButton;
TextView tvView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    myFirstButton = (Button) findViewById(R.id.myFirstButton);
    mySecondButton = (Button) findViewById(R.id.mySecondButton);
    myThirdButton = (Button) findViewById(R.id.myThirdButton);

    tvView = (TextView) findViewById(R.id.produceText1);

    //if (tvView == null) { Log.w("", "TextView is null"); }

如果我注释掉我的应用程序运行的底部三行(但没有任何按钮功能)。我认为“新”可能与它有关吗?

myFirstButton.setOnClickListener(new MyOwnOnClickListener(tvView, "myFirstButton"));
mySecondButton.setOnClickListener(new MyOwnOnClickListener(tvView, "mySecondButton"));
myThirdButton.setOnClickListener(new MyOwnOnClickListener(tvView, "myThirdButton"));

MyOwnOnClickListner.java

package com.example.figurehowtodo;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MyOwnOnClickListener extends Activity implements OnClickListener{
    //int id; //comment line out in order to make it work
    //TextView id3;

    /* 
    *
    *MainActivity caller;
    *public MyOwnOnClickListener() {
    *€  addiTion();
    *   IGNORE THIS BIT
    *} 
    *
    */

    TextView outputBoxId;
    String re_id_button_name;

    Button myFirstButton;
    Button mySecondButton;
    Button myThirdButton;
    TextView tvView3;

    public MyOwnOnClickListener(TextView id2, String id) {
        this.outputBoxId = id2;
        this.re_id_button_name = id;

        myFirstButton = (Button) findViewById(R.id.myFirstButton);
        mySecondButton = (Button) findViewById(R.id.mySecondButton);
        myThirdButton = (Button) findViewById(R.id.myThirdButton);
    }

    public void onClick(View re_id_button_name) {
        //tvView3 = (TextView) findViewById(R.id.produceText1);
        outputBoxId.setText("it worked!!!");
    }

    /*
     * -------IGNORE---------
    if(re_id_button_name.equals(myFirstButton)){
        addiTion();
    }else{return;}

    public void addiTion(){
        //id = v.getId(); //comment line out in order to make it work

        outputBoxId.setText("YOU CLICKED THE FIRST BUTTON!");
    }
    -------IGNORE---------*/
}

logcat的:

03-28 21:56:24.388: E/AndroidRuntime(1256): FATAL EXCEPTION: main
03-28 21:56:24.388: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.figurehowtodo/com.example.figurehowtodo.MainActivity}: java.lang.NullPointerException
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.os.Looper.loop(Looper.java:123)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at dalvik.system.NativeStart.main(Native Method)
03-28 21:56:24.388: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.Activity.findViewById(Activity.java:1647)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at com.example.figurehowtodo.MyOwnOnClickListener.<init>(MyOwnOnClickListener.java:31)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at com.example.figurehowtodo.MainActivity.onCreate(MainActivity.java:25)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 21:56:24.388: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-28 21:56:24.388: E/AndroidRuntime(1256):     ... 11 more
03-28 21:56:24.408: W/ActivityManager(62):   Force finishing activity com.example.figurehowtodo/.MainActivity
03-28 21:56:24.917: W/ActivityManager(62): Activity pause timeout for HistoryRecord{407eda90 com.example.figurehowtodo/.MainActivity}
03-28 21:56:30.757: D/dalvikvm(233): GC_EXPLICIT freed 8K, 55% free 2597K/5703K, external 1625K/2137K, paused 53ms

2 个答案:

答案 0 :(得分:0)

简单写

        Button myFirstButton = (Button) findViewById(R.id.myFirstButton);
        myFirstButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click   

            }
        });

对其他按钮也一样。

答案 1 :(得分:0)

这里的主要问题是您的自定义OnClickListener不应该extends Activity,因为它不是Activity。而且,它没有layout来保存你试图膨胀的idView

这就是崩溃的原因,因为当您尝试在NPE上设置listener时,您会收到View。您需要在实际listeners中为Activity设置layoutfindViewById() myBtn.setOnClickListener(new View.OnClickListener());

所以,而不是像

那样
myBtn.setOnClickListener(new MyOwnOnClickListener());

你会有

Context

修改

试试这个,使用传入的View中的View获取另一个public MyOwnOnClickListener(TextView id2, String id) { myFirstButton = (Button) id2.getContext().findViewById(R.id.myFirstButton); } 。我不肯定这会起作用,我现在没时间测试它,但让我们看看。

{{1}}