私のアプリケーションをエミュレータで実行しようとするたびに、2番目の画面(advert.xml)につながるスプラッシュ画面(splash.xml)の後に、私は致命的な例外をLogCat。エミュレータで実行されているアプリもクラッシュします(予期せず)。私は致命的な例外が2番目の画面だと思ったが、代わりに3番目の画面だと言います。ここにログがあります:アンドロイド:致命的な例外:XMLファイル
01-08 23:03:35.093: D/AndroidRuntime(287): Shutting down VM
01-08 23:03:35.093: W/dalvikvm(287): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-08 23:03:35.163: E/AndroidRuntime(287): FATAL EXCEPTION: main
01-08 23:03:35.163: E/AndroidRuntime(287): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.revagar.android.niles/com.revagar.android.niles.Advert}: java.lang.NullPointerException
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.os.Looper.loop(Looper.java:123)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-08 23:03:35.163: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 23:03:35.163: E/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521)
01-08 23:03:35.163: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-08 23:03:35.163: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-08 23:03:35.163: E/AndroidRuntime(287): at dalvik.system.NativeStart.main(Native Method)
01-08 23:03:35.163: E/AndroidRuntime(287): Caused by: java.lang.NullPointerException
01-08 23:03:35.163: E/AndroidRuntime(287): at com.revagar.android.niles.Advert.onCreate(Advert.java:18)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-08 23:03:35.163: E/AndroidRuntime(287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-08 23:03:35.163: E/AndroidRuntime(287): ... 11 more
01-08 23:03:38.663: I/Process(287): Sending signal. PID: 287 SIG: 9
メインは、私の3番目の画面です。私はそれをぶち壊していたが、以前は何の問題もなかった。ここでは、XMLは、メインです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/appbg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Club Finder"
android:textSize="28dp"
android:layout_gravity="center"
android:textColor="#bc0d0d"
android.id="@+id/tvDisplay"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:gravity="center"
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Search by Club Name"
android:textSize="18dp"
android.id="@+id/bSbN"
android:background="@drawable/button1"
android:textColor="#bc0d0d"
android:layout_marginTop="70dp"
/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Search by Location"
android:textSize="18dp"
android.id="@+id/bSbL"
android:background="@drawable/button1"
android:textColor="#bc0d0d"
android:layout_marginTop="60dp"
/>
私は実際に私が作成したボタンをテストしていました。ボタンは2番目の画面にあり、クリックすると3番目の画面に移動します。ここでは、ボタンを含む第2の画面のJavaコードは次のとおりです。
package com.lol.android.cop;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Advert extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.advert);
Button bSkipty = (Button) findViewById(R.id.bSKIP);
bSkipty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.lol.android.cop.MAINACTIVITY"));
}
});
}
}
誰かが私はこの問題を解決する手助けができれば、私は永遠に感謝されます。
編集:ここではadvert.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="match_parent"
android:orientation="vertical"
android:background="@drawable/advert">
<Button
android:layout_width="68dp"
android:layout_height="38dp"
android:layout_gravity="right"
android:text="Skip"
android.id="@+id/bSKIP"
android:background="@drawable/button1"
android:textColor="#FFFFFF"
android:layout_marginTop="2dp"
/>
<Button
android:layout_width="68dp"
android:layout_height="38dp"
android:layout_gravity="right"
android:text="Apply"
android.id="@+id/bAPPLY"
android:background="@drawable/button1"
android:textColor="#FFFFFF"
android:layout_marginTop="4dp"
/>
</LinearLayout>
私はできませんそのXMLにid "bSKIP"のボタンを見つけてください。正しいコードを表示していますか? – tidbeck
同じ考えがありました。 '' R.layout.advert''に、指定されたidを持つ要求されたButtonが含まれていることを確認してください。 – harism
なぜNPEがあるのか、間違ったIDが見つかりました – nandeesh