2013-08-25 57 views
1

main.java應用程序意外停止。錯誤引起的:顯示java.lang.NullPointerException

package com.learnactivities; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(Main.this, Second.class)); 

     } 
    }); 
} 

}

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Main" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button1" /> 

</RelativeLayout> 

Second.java

package com.learnactivities; 

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

public class Second extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 
} 

}

second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Second" > 

<TextView 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="16dp" 
    android:layout_marginTop="15dp" 
    android:text="@string/nd" /> 

</RelativeLayout> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.learnactivities" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.learnactivities.Main" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".second" /> 
</application> 

</manifest> 

我m到處以下錯誤。我可以看到由java.lang.NullPointerException引起的錯誤。我在這裏和那裏做了最好的搜索,但無法得到解決方案。普茨幫助我解決這個問題嗎?

08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM 
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main 
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123) 
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method) 
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521) 
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method) 
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException 
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19) 
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)` 
`08-25 19:08:44.472: E/AndroidRuntime(276):  at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)` 
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more 
+0

我在manifest.xml中有這個..我應該在哪裏小號...?任何具體的答案plz? – score

回答

2

R.id.button1聲明中second.xml,但你正在尋找這裏面activity.xml。所以在Main,當你做

Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() 

findViewById被返回null

+2

清單中的活動條目也是錯誤的。類名是'秒',但入口是''區分大小寫我猜 – Raghunandan

+0

是的,這是真的。但它不是異常的原因 – Blackbelt

+0

blackbelt:你能不能特別指出......我應該在這裏做什麼? – score

0

在主類試試這個..

TextView b = (TextView) findViewById(R.id.textView1); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(Main.this, Second.class)); 

     } 
    }); 

,也像下面一行

<activity android:name=".Second" /> 
明顯變化
相關問題