2014-03-07 113 views
0

這是我的問題。我創建了一個按鈕ID:「fibo1」。此按鈕屬於「CList」活動。單擊按鈕時,我想打開一個名爲「」斐波那契」的在Android應用程序中點擊一個按鈕打開新的活動

XML另一活動「欄列表的Java上打開「列表中的應用崩潰的

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/fibo1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:onClick="sendMessage" 
      android:text="Fibo" /> 

欄列表

public class CList extends Activity { 
    Button read1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list); 


     read1 = (Button) findViewById (R.id.fibo1); 
     read1.setOnClickListener (new OnClickListener(){ 
      @Override 
      public void onClick (View v){ 
       Intent intent = new Intent (CList.this,Fibonacci.class); 
       startActivity (intent); 
       finish(); 
      } 
     }); 
    } 

'Log Activity給出錯誤Fatal Exception :main。 我已經將這兩項活動都添加到了Android Manifest中。 我只是一個初學者,所以請原諒我,如果我的術語不正確

這裏是登錄貓

03-07 19:33:29.931: E/AndroidRuntime(19565): FATAL EXCEPTION: main 

03-07 19:33:29.931: E/AndroidRuntime(19565): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geek.cprogramspopular/com.geek.cprogramspopular.CList}: java.lang.NullPointerException 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.access$700(ActivityThread.java:150) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.os.Handler.dispatchMessage(Handler.java:99) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.os.Looper.loop(Looper.java:137) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.main(ActivityThread.java:5283) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at java.lang.reflect.Method.invokeNative(Native Method) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at java.lang.reflect.Method.invoke(Method.java:511) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at dalvik.system.NativeStart.main(Native Method) 

03-07 19:33:29.931: E/AndroidRuntime(19565): Caused by: java.lang.NullPointerException 

03-07 19:33:29.931: E/AndroidRuntime(19565): at com.geek.cprogramspopular.CList.onCreate(CList.java:23) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.Activity.performCreate(Activity.java:5283) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 

03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 

03-07 19:33:29.931: E/AndroidRuntime(19565): ... 11 more 
+0

發佈您的logcat .. –

+0

發佈日誌輸出? – mach

+0

發佈完整的代碼和錯誤日誌 –

回答

0

你只是從你的XML文件中刪除這條線,你需要不完的意圖呼喚你的活動......

android:onClick="sendMessage" 
read1.setOnClickListener (new OnClickListener(){ 
     @Override 
     public void onClick (View v){ 
      Intent intent = new Intent (CList.this,Fibonacci.class); 
      startActivity (intent);     
     } 
    }); 
+0

我試過了。問題依然存在。我認爲問題出在'Clist'佈局上,Eclipse中對它所做的更改沒有反映在應用程序中。 – John918

0

使用以下碼 -

public void sendMessage(View v) { 

    Intent intent = new Intent(CList.this, Fibonacci.class); 
    startAcitivty(intent); 
} 

onCreate(Bundle savedInstanceState)方法取而代之, -

read1.setOnClickListener (new OnClickListener(){ 
     @Override 
     public void onClick (View v){ 
      Intent intent = new Intent (CList.this,Fibonacci.class); 
      startActivity(intent); 
      finish(); 
} 

在完成此操作後,您還可以刪除Button read1;read1 = (Button) findViewById (R.id.fibo1);

+0

這不是問題所在。我設法找到了一個解決辦法。不管怎麼說,還是要謝謝你。 – John918

相關問題