2014-03-28 36 views
0

該應用程序強制關閉,甚至沒有開始,請參閱下面的logcat的信息和(應用工程時,下面3行註釋掉)錯誤使用自定義OnClickListener

  • 也說,在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 
+0

如果你把它放回去,會發生什麼:''if(tvView == null){Log.w(「」,「TextView is null」); ''。它是否爲空?似乎對我來說。 – puelo

+0

它沒有太大的區別,仍然強制關閉......我的代碼寫入方式有問題嗎? – sudoman

+1

不,我的意思是如果logcat在崩潰之前顯示「TextView爲空」。 – puelo

回答

0

這裏的主要問題是您的自定義OnClickListener不應該extends Activity,因爲它不是Activity。而且,它沒有layout,它包含你試圖膨脹的的。

這就是爲什麼它崩潰,因爲當您嘗試在View上設置listener時,您會收到NPE。您需要在實際Activity上設置listeners,在和findViewById()之間。

所以,而不必像

myBtn.setOnClickListener(new View.OnClickListener()); 

你會

myBtn.setOnClickListener(new MyOwnOnClickListener()); 

編輯

試試這個,使用ContextView傳遞拿到其他View。我不積極,這將工作,我沒有時間在這一刻進行測試,但讓我們看看。

public MyOwnOnClickListener(TextView id2, String id) { 

    myFirstButton = (Button) id2.getContext().findViewById(R.id.myFirstButton); 
} 
+0

完美!擺脫了「擴展活動」的伎倆,但有一個問題,在我的'MyOwnOnClickListener'類中...如果我想獲得一個視圖,我將如何去做呢?考慮到'findViewById'由於'擴展活動'不存在而不再有效。 – sudoman

+0

你應該可以在類 – codeMagic

+0

的構造函數中正確傳遞它,但是說我想將通過參數接收到的id與另一個不想通過prams的id進行比較(因爲需要對它進行比較不止一個身份證......不能把所有人都放在普通身上)。 – sudoman

0

簡單寫

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

      } 
     }); 

,也爲其他按鈕的功能相同。

+0

我的應用程序的關鍵是將onClickListener運行到另一個類中,以便以後可以通過各種方法調用,同時仍能夠瀏覽我的代碼,但感謝提示,儘管:) – sudoman

+0

然後如果您將參數傳遞給button,並在onClick方法中使用,那麼效果會更好。 – Ranjit