我正在构建一个应用程序,但那里存在问题。当我尝试运行我的应用程序时,出现以下错误:

>09-16 14:07:35.768: E/AndroidRuntime(21484): FATAL EXCEPTION: main
>09-16 14:07:35.768: E/AndroidRuntime(21484): java.lang.RuntimeException: Unable to start activity ComponentInfo{lt.prasom/lt.prasom.MainMenu}: java.lang.NullPointerException
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.os.Handler.dispatchMessage(Handler.java:99)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.os.Looper.loop(Looper.java:130)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread.main(ActivityThread.java:3683)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at java.lang.reflect.Method.invokeNative(Native Method)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at java.lang.reflect.Method.invoke(Method.java:507)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at dalvik.system.NativeStart.main(Native Method)
>09-16 14:07:35.768: E/AndroidRuntime(21484): Caused by: java.lang.NullPointerException
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at lt.prasom.MainMenu.onCreate(MainMenu.java:19)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
>09-16 14:07:35.768: E/AndroidRuntime(21484):   ... 11 more


我的清单:

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainMenu"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".GoodOffers" android:theme="@android:style/Theme.Black.NoTitleBar" />
        <activity android:name=".Search" android:theme="@android:style/Theme.Black.NoTitleBar" />
        <activity android:name=".Groups" android:theme="@android:style/Theme.Black.NoTitleBar" />
        <activity android:name=".Specialmenu" android:theme="@android:style/Theme.Black.NoTitleBar" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>


我的活动在lt.prasom包中。当我重命名程序包名称时出现错误。

MainMenu.java:

package lt.prasom;

import lt.prasom.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button search = (Button) findViewById(R.id.searchbutt);
        search.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                Intent i = new Intent(MainMenu.this.getApplicationContext(), Search.class);
                startActivity(i);

            }

        });
        Button groups = (Button) findViewById(R.id.groupsbutt);
        groups.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                Intent i = new Intent(MainMenu.this.getApplicationContext(), Groups.class);
                startActivity(i);

            }

        });
        Button goods = (Button) findViewById(R.id.specialbutt);
        goods.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0) {
                Intent i = new Intent(MainMenu.this.getApplicationContext(), Specialmenu.class);
                startActivity(i);

            }

        });
    }
}


Main.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:paddingTop="10dp"
    android:orientation="vertical"
     xmlns:ui="http://schemas.android.com/apk/res/lt.prasom" xmlns:app="http://schemas.android.com/apk/res/lt.prasom"

    android:background="#f8f9fe" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        android:layout_gravity="center"
        android:src="@drawable/logo" />

<Button android:id="@+id/goodsbutt"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Geri pasiūlymai"

                    />
<Button android:id="@+id/akcijabutt"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Akcijinės prekės"

                    />
<Button android:id="@+id/newsbutt"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="Naujos prekės"

                    />



</LinearLayout>


谢谢!

最佳答案

当前活动的main.xml中未声明searchbutt id。您必须通过setContentView()选择当前显示的元素

因此Button search = (Button) findViewById(R.id.searchbutt);搜索是null

search.setOnClickListener(new OnClickListener(){


引发异常

关于android - E/AndroidRuntime(21484)无法启动 Activity ComponentInfo {lt.prasom/lt.prasom.MainMenu}:java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12446526/

10-12 06:05