該應用程序強制關閉,甚至沒有開始,請參閱下面的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
如果你把它放回去,會發生什麼:''if(tvView == null){Log.w(「」,「TextView is null」); ''。它是否爲空?似乎對我來說。 – puelo
它沒有太大的區別,仍然強制關閉......我的代碼寫入方式有問題嗎? – sudoman
不,我的意思是如果logcat在崩潰之前顯示「TextView爲空」。 – puelo