2016-07-14 59 views
-1

我正在製作一個正確的馬電池主食樣式密碼生成器。我有它設置,以便有一個TextView,當你點擊它,它切換這樣的活動:
TextView moreInfo = (TextView) findViewById(R.id.moreinfo); moreInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Description.class); startActivity(intent); }; });
但是,當我運行該程序,然後單擊詳細信息TextView的,我得到這個錯誤:
07-14 12:34:15.930 2295-2295/com.example.bobmo.correcthorsebatterystaple E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bobmo.correcthorsebatterystaple, PID: 2295 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bobmo.correcthorsebatterystaple/com.example.bobmo.correcthorsebatterystaple.Description}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setClickable(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setClickable(boolean)' on a null object reference at com.example.bobmo.correcthorsebatterystaple.Description.onCreate(Description.java:16) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
...並在電話上說,該程序已停止。有沒有什麼辦法解決這一問題?謝謝!在Android中切換活動奇怪的錯誤

+0

沒有一個奇怪的任何視圖問題你有一個NullPointerException,你也沒有點擊一個按鈕,你點擊一個textview。 – basic

回答

2
TextView moreInfo = (TextView) findViewById(R.id.moreinfo); 

findViewById返回null,因爲它並沒有發現在當前活動內容視圖的視圖,你可能沒有設置正確的佈局或佈局不包含id爲R.id.moreinfo

+0

我仔細檢查了我在XML中定義了textview的位置。這是我的代碼:
'' –

+0

@BobMonkeywarts你打電話給setContentView(R.layout ?)在findViewById之前? – Ladas125

+0

是的,我在findviewbyid之前調用setcontentview。 –