-1

启动我的应用程序时遇到问题,我想知道究竟是什么问题。

我在日志猫中得到了这个:

09-08 12:40:35.690: W/dalvikvm(1382): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-08 12:40:35.740: E/AndroidRuntime(1382): FATAL EXCEPTION: main
09-08 12:40:35.740: E/AndroidRuntime(1382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yahya.myfirstapp/com.yahya.myfirstapp.Main}: java.lang.NullPointerException
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.os.Looper.loop(Looper.java:123)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at java.lang.reflect.Method.invoke(Method.java:507)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at dalvik.system.NativeStart.main(Native Method)
09-08 12:40:35.740: E/AndroidRuntime(1382): Caused by: java.lang.NullPointerException
09-08 12:40:35.740: E/AndroidRuntime(1382):     at com.yahya.myfirstapp.Main.onCreate(Main.java:19)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-08 12:40:35.740: E/AndroidRuntime(1382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-08 12:40:35.740: E/AndroidRuntime(1382):     ... 11 more
09-08 12:40:41.260: I/Process(1382): Sending signal. PID: 1382 SIG: 9

这是 Main.java

package com.yahya.myfirstapp;

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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

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

            }
        });
    }
}

而这个 Second.java

package com.yahya.myfirstapp;

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);
        }
}

activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/welcome" />

</RelativeLayout>

第二个.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">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/test_secondactivity" />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/welcome" />

</LinearLayout>
4

2 回答 2

3

更正这一行:

Button b = (Button) findViewById(android.R.id.button1);

成为:

Button b = (Button) findViewById(R.id.button1);

android.R用于访问嵌入在 Android OS 中的内置系统资源,并且没有调用资源button1

于 2012-09-10T12:45:39.673 回答
0

确保在 androidmanifest.xml 中标识了您的类

于 2012-09-10T12:45:56.463 回答