我有一個非常簡單的代碼,因爲我是Java和Android的新手。所以,我有這個按鈕,點擊時,應該在兩個文本視圖之間切換文本。 我有我的Android手機插入調試模式,所以我的應用程序直接在它上面運行。然而,它一發布就會崩潰,並說,「不幸的是,已經停止」。在eclipse中,android應用程序在聲明變量時崩潰
這裏是我的代碼:
public class ConvertItAll extends Activity {
String temp;
TextView tv1 = (TextView) findViewById(R.id.tvUnit1);
TextView tv2 = (TextView) findViewById(R.id.tvUnit2);
EditText et1=(EditText) findViewById(R.id.etInput);
TextView tv3=(EditText) findViewById(R.id.tvOutput);
Button b1=(Button) findViewById(R.id.bEdit1);
Button b2=(Button) findViewById(R.id.bEdit2);
Button b3 = (Button) findViewById(R.id.bSwap);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
temp = tv1.getText().toString();
tv1.setText(tv2.getText().toString());
tv2.setText(temp);
}
});
}
}
的錯誤,當我看到logcat中,發生在我聲明並初始化變量監視線。所以,我試圖評論這些聲明(當然也沒有使用這些變量),並且應用程序加載正常,我可以看到佈局。
有人可以幫忙嗎?如果我不能初始化變量,我將如何使用它們?
下面是logcat的結果:
• 09-29 23:42:10.788: I/Process(600): Sending signal. PID: 600 SIG: 9
• 09-29 23:42:27.846: E/Trace(670): error opening trace file: No such file or directory (2)
• 09-29 23:42:28.536: D/dalvikvm(670): GC_FOR_ALLOC freed 70K, 3% free 8051K/8259K, paused 51ms, total 53ms
• 09-29 23:42:28.556: I/dalvikvm-heap(670): Grow heap (frag case) to 8.827MB for 960016-byte allocation
• 09-29 23:42:28.776: D/dalvikvm(670): GC_CONCURRENT freed 1K, 3% free 8987K/9223K, paused 78ms+6ms, total 222ms
• 09-29 23:42:28.776: D/dalvikvm(670): WAIT_FOR_CONCURRENT_GC blocked 8ms
• 09-29 23:42:29.106: D/gralloc_goldfish(670): Emulator without GPU emulation detected.
• 09-29 23:42:29.966: D/AndroidRuntime(670): Shutting down VM
• 09-29 23:42:29.966: W/dalvikvm(670): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
• 09-29 23:42:29.996: E/AndroidRuntime(670): FATAL EXCEPTION: main
• 09-29 23:42:29.996: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.allinoneconverter/com.example.allinoneconverter.ConvertItAll}: java.lang.NullPointerException
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.access$600(ActivityThread.java:130)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.os.Handler.dispatchMessage(Handler.java:99)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.os.Looper.loop(Looper.java:137)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.main(ActivityThread.java:4745)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.reflect.Method.invokeNative(Native Method)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.reflect.Method.invoke(Method.java:511)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at dalvik.system.NativeStart.main(Native Method)
• 09-29 23:42:29.996: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.Activity.findViewById(Activity.java:1825)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at com.example.allinoneconverter.ConvertItAll.<init>(ConvertItAll.java:12)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.Class.newInstanceImpl(Native Method)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at java.lang.Class.newInstance(Class.java:1319)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
• 09-29 23:42:29.996: E/AndroidRuntime(670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
• 09-29 23:42:29.996: E/AndroidRuntime(670): ... 11 more
另外你爲什麼要鑄造一個TextView來的EditText? 「TextView tv3 =(EditText)findViewById(R.id.tvOutput);」 – Sush
@visheshchandra - 不要編輯問題以回答問題。這不是正確的做法。如果您想回答,請在下面執行。 –
@Sush那麼這似乎是一個打字(而不是複製和粘貼)的錯誤。感謝您指出! – madebysid