2013-07-25 48 views
1

我在我的java文件中的以下初始化:的Android應用程序崩潰是由於加入了佈局

Button btnCalc = (Button) findViewById(R.id.btnCalculate); 
final Button btnClearWin = (Button) findViewById(R.id.btnClear); 
final Button btnSaveTrip = (Button) findViewById(R.id.btnSave); 
final EditText nameOfInf = (EditText)findViewById(R.id.etName); 
final EditText tollAmount = (EditText)findViewById(R.id.etToll); 
final EditText showLog = (EditText)findViewById(R.id.etShowLog); 
showLog.setFocusable(false); 

final View lineView = (View) findViewById(R.id.vwLine); 
final TextView tvTotalLabel = (TextView) findViewById(R.id.tvTotal); 
final TextView tvTotalAmountLabel = (TextView) findViewById(R.id.tvTotalAmount); 

final TextView tvNameLabel = (TextView) findViewById(R.id.tvName); 
final TextView tvTollLabel = (TextView) findViewById(R.id.tvToll); 

final RadioGroup rgTypeOfInf = (RadioGroup) findViewById(R.id.rgType); 
final RadioGroup rgTypeOfTrip = (RadioGroup) findViewById(R.id.rgTripType); 

本來一切工作正常,直到我把一些對象到不同的佈局,是在main不再佈局文件,現在我的應用程序FC打開時。

以下是不同的佈局,result.xml。我必須分別初始化它們嗎?

final EditText showLog = (EditText)findViewById(R.id.etShowLog); 
showLog.setFocusable(false); 
final Button btnClearWin = (Button) findViewById(R.id.btnClear); 
final Button btnSaveTrip = (Button) findViewById(R.id.btnSave); 

當我註釋掉上述四行時,應用程序打開就好了。

的logcat:

07-25 10:27:27.955: E/AndroidRuntime(13791): FATAL EXCEPTION: main 
07-25 10:27:27.955: E/AndroidRuntime(13791): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.test.testing.MainActivity}: java.lang.NullPointerException 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread.access$600(ActivityThread.java:150) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.os.Looper.loop(Looper.java:137) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread.main(ActivityThread.java:5195) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at java.lang.reflect.Method.invokeNative(Native Method) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at java.lang.reflect.Method.invoke(Method.java:511) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at dalvik.system.NativeStart.main(Native Method) 
07-25 10:27:27.955: E/AndroidRuntime(13791): Caused by: java.lang.NullPointerException 
07-25 10:27:27.955: E/AndroidRuntime(13791): at com.test.testing.MainActivity.onCreate(MainActivity.java:51) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.Activity.performCreate(Activity.java:5104) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
07-25 10:27:27.955: E/AndroidRuntime(13791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260) 
07-25 10:27:27.955: E/AndroidRuntime(13791): ... 11 more 
07-25 10:27:27.963: W/ActivityManager(381): Force finishing activity com.test.testing/.MainActivity 
07-25 10:27:28.517: W/ActivityManager(381): Activity pause timeout for ActivityRecord{4163fe28 u0 com.test.testing/.MainActivity} 
07-25 10:27:28.666: I/qtaguid(381): Failed write_ctrl(s 0 10116) res=-1 errno=1 
07-25 10:27:28.666: W/NetworkManagementSocketTagger(381): setKernelCountSet(10116, 0) failed with errno -1 
07-25 10:27:38.666: W/ActivityManager(381): Activity destroy timeout for ActivityRecord{4163fe28 u0 com.test.testing/.MainActivity} 
07-25 10:28:00.166: D/dalvikvm(2069): GC_CONCURRENT freed 3198K, 52% free 13534K/27904K, paused 3ms+3ms, total 29ms 
07-25 10:28:00.166: D/dalvikvm(2069): WAIT_FOR_CONCURRENT_GC blocked 23ms 
+0

你是什麼意思與「比其他主」? – Warpzit

+0

請發佈logcat輸出以及 –

+0

您可以初始化佈局xml中設置爲該活動的視圖。如果它在不同的xml中,並且你嘗試初始化你的NPE。 – Raghunandan

回答

1

如果要使用除主佈局以外的其他佈局的視圖,則需要對該佈局進行充氣。如果你不膨脹,直接用自己的觀點,它會給NPE

示例代碼:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
RelativeLayout mFrame = (RelativeLayout)inflater.inflate(R.layout.results, null); 
//Now you can use/reference its resources/views etc. 
final EditText showLog = (EditText)mFrame.findViewById(R.id.etShowLog); 
showLog.setFocusable(false); 
final Button btnClearWin = (Button) mFrame.findViewById(R.id.btnClear); 
final Button btnSaveTrip = (Button) mFrame.findViewById(R.id.btnSave); 
+0

我會在onCreate()方法下添加該對象嗎? – Si8

+1

是的,你可以,確保它在使用該佈局中定義的視圖之前。我已經更新了代碼。另外請確保您使用適當的mFrame佈局。如果它的LinearLayout將RelativeLayout更改爲LinearLayout – Manu

+0

感謝兄弟。這解決了這個問題:) – Si8

1

我從你的代碼,看到的是 「可能」 只有這個代碼將產生錯誤:

showLog.setFocusable(false)

嘗試檢查空第一設置任何值

if (showLog != null) showLog.setFocusable(false);

+0

你是對的,它不會再崩潰了。 – Si8

+1

要完成分配,您需要在充氣合適佈局的活動上設置showLog.setFocusable(false)。因此,你會得到它的權利。 –

+0

我確實有另一個問題。問題是,我試圖在單擊當前佈局文件時在不同的佈局文件中顯示一些值。你有我可以發送你的Java代碼的電子郵件嗎? – Si8

0

在移動或重命名文件或資源之後,必須清理並構建新引用的項目。

+0

這個與NPE無關。 – Raghunandan

+0

-1:這與錯誤無關。 –

+0

當然,如果對象無法初始化,您會得到一個NPE – JavaDM

0

對於使用setcontentview(佈局)時的活動。只有您可以參考您的活動的xml佈局視圖。對於其他XML,您必須將該XML充當視圖。