2012-12-29 96 views
1

我讀過很多關於致命的異常,但非以前的職位幫助我致命異常:主要在Android的

所以我非常新的Android和我建立一個非常簡單的應用程序,允許u到點擊一個按鈕,並把你ü到另一個頁面,你可以點擊其他按鈕,但一旦我運行它在模擬器上我得到一個「應用程序進程已意外停止」和logcat給我一個致命的異常 代碼是

package my.first.pro; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
    import android.view.*; 
    import android.view.View.OnClickListener; 

    public class MyfirstproActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    View button = this.findViewById(R.layout.main); 
    button.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Intent i = new Intent (MyfirstproActivity.this, Second.class); 
     } 
    }); 
} 

}

t他XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 


    <Button 
    android:id="@+id/button" 
    android:layout_width="233dp" 
    android:layout_height="wrap_content" 
    android:text="view health care services " /> 

    </LinearLayout> 

第二活動

package my.first.pro; 

    import android.app.Activity; 
    import android.os.Bundle; 

    public class Second extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // TODO Auto-generated method stub 
} 

}

第二活性XML文件

<?xml version="1.0" encoding="UTF-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="The following the health care services provided :" /> <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     </TextView> 


    <Button 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:text="General"/> 
    </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 

    <Button 

     android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:text="physciatric"/> 
    </LinearLayout> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
     > 

    <Button 

     android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:text="pediatric"/> 
    </LinearLayout> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 

     <Button 

     android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:text="dental"/> 
     </LinearLayout> 
     </LinearLayout> 

的logcat的是

12-29 11:15:10.508: E/AndroidRuntime(438): FATAL EXCEPTION: main 
    12-29 11:15:10.508: E/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activityComponentInfo{my.first.pro/my.first.pro.MyfirstproActivity}: java.lang.NullPointerException 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.os.Handler.dispatchMessage(Handler.java:99) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.os.Looper.loop(Looper.java:123) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.app.ActivityThread.main(ActivityThread.java:3683) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at java.lang.reflect.Method.invokeNative(Native Method) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at java.lang.reflect.Method.invoke(Method.java:507) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at dalvik.system.NativeStart.main(Native Method) 
    12-29 11:15:10.508: E/AndroidRuntime(438): Caused by: java.lang.NullPointerException 
    12-29 11:15:10.508: E/AndroidRuntime(438): at my.first.pro.MyfirstproActivity.onCreate(MyfirstproActivity.java:16) 
    12-29 11:15:10.508: E/AndroidRuntime(438):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    12-29 11:15:10.508: E/AndroidRuntime(438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
    12-29 11:15:10.508: E/AndroidRuntime(438): ... 11 more 
    12-29 11:15:24.829: E/AndroidRuntime(448): FATAL EXCEPTION: main 
    12-29 11:15:24.829: E/AndroidRuntime(448): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.first.pro/my.first.pro.MyfirstproActivity}: java.lang.NullPointerException 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.os.Handler.dispatchMessage(Handler.java:99) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.os.Looper.loop(Looper.java:123) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread.main(ActivityThread.java:3683) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at java.lang.reflect.Method.invokeNative(Native Method) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at java.lang.reflect.Method.invoke(Method.java:507) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at dalvik.system.NativeStart.main(Native Method) 
    12-29 11:15:24.829: E/AndroidRuntime(448): Caused by: java.lang.NullPointerException 
    12-29 11:15:24.829: E/AndroidRuntime(448): at my.first.pro.MyfirstproActivity.onCreate(MyfirstproActivity.java:16) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at andr oid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
    12-29 11:15:24.829: E/AndroidRuntime(448): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
    12-29 11:15:24.829: E/AndroidRuntime(448): ... 11 more 

回答

3

這裏

View button = this.findViewById(R.layout.main); 

你正在努力尋找在錯誤的方式在活動佈局視圖。在佈局XML尋找查看正確的方法是:

Button button = (Button)findViewById(R.id.button); 

始終申報所有UI控件作爲一流水平的區域,而不是內部的方法,因爲如果你聲明在類級別的所有元素,那麼你可以在整個活動進行訪問。

編輯:

始終使用該活動模板爲更好的UI元素的無障礙在整個活動聲明或初始化UI元素:

public class MyfirstproActivity extends Activity { 
    /** Called when the activity is first created. */ 

    //******* ALWAYS DECLARE UI ELEMENTS HERE ********** // 

Button button ; //<<< for example 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //** ALWAYS SET layout FOR ACTIVITY BEFORE INITIALIZING UI ELEMENTS ** 
    setContentView(R.layout.main); 

    //**** ALWAYS INITILIZING UI ELEMENTS IN SIDE onCreate of Activity *** // 
    button = (Button)findViewById(R.id.button); 

    } 

} 
+0

+1準確..... – vnshetty

+0

感謝這麼多,解決它也 – user1936104

+0

做u知道爲什麼我得到了一個空白頁面,當我點擊我的按鈕雖然我應該看到我的其他活動(第二個)其中包括文本和其他按鈕 – user1936104

1

你有這樣的

後添加以下語句
Intent i = new Intent (MyfirstproActivity.this, Second.class); 

startActivity(i); //this will start the new activity. 
+0

謝謝你們所有的回答,你也知道爲什麼我得到一個空白的屏幕,當我點擊我的按鈕,雖然我應該看到我的其他活動(第二次)其中包括文本和其他按鈕小號 – user1936104

0

更換生產線

View button = this.findViewById(R.layout.main); 

這樣:

View button = this.findViewById(R.id.button);