에뮬레이터에서 내 응용 프로그램을 실행할 때마다 두 번째 화면 (advert.xml)으로 연결되는 스플래시 화면 (splash.xml) 이후에 필자는 LogCat. 에뮬레이터에서 실행중인 앱도 예기치 않게 중단됩니다. 나는 치명적인 예외가 두 번째 화면이 될 것이라고 생각했지만 그 대신 내 세 번째 화면이라고합니다. 로그는 다음과 같습니다.ANDROID : 치명적인 예외 : 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
주요한 예외는 내 세 번째 화면입니다. 나는 그걸로 장난하고 있었지만 더 일찍 문제가 없었습니다. 다음은 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"
/>
. 버튼은 두 번째 화면에 있으며 클릭하면 세 번째 화면으로 이동합니다. 여기에 버튼을 포함하는 두 번째 화면에 대한 자바 코드는 다음과 같습니다 누군가가 나에게이 문제를 해결하는 데 도움 수 있다면
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"));
}
});
}
}
, 나는 영원히 감사 할 것이다.
편집 :
<?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에서 "bSKIP"id를 가진 버튼을 찾으십시오. 올바른 코드를 표시하고 있습니까? – tidbeck
같은 생각이 들었습니다. ''R.layout.advert''가 주어진 id를 가진 요청 된 Button을 포함하고 있는지 확인하십시오. – harism
그리고 왜 NPE가 있는지, 잘못된 ID가 발견되었습니다. – nandeesh