大家好,我在模拟器中运行应用程序,但是遇到错误,每当选择列表项时,该应用程序就会崩溃。我得到的错误日志是:

06-07 17:41:50.150: E/AndroidRuntime(624): FATAL EXCEPTION: main
06-07 17:41:50.150: E/AndroidRuntime(624): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dapps.factpalooza/com.dapps.factpalooza.animal}: java.lang.NullPointerException
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.os.Looper.loop(Looper.java:137)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-07 17:41:50.150: E/AndroidRuntime(624):  at java.lang.reflect.Method.invokeNative(Native Method)
06-07 17:41:50.150: E/AndroidRuntime(624):  at java.lang.reflect.Method.invoke(Method.java:511)
06-07 17:41:50.150: E/AndroidRuntime(624):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-07 17:41:50.150: E/AndroidRuntime(624):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-07 17:41:50.150: E/AndroidRuntime(624):  at dalvik.system.NativeStart.main(Native Method)
06-07 17:41:50.150: E/AndroidRuntime(624): Caused by: java.lang.NullPointerException
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.Activity.findViewById(Activity.java:1825)
06-07 17:41:50.150: E/AndroidRuntime(624):  at com.dapps.factpalooza.animal.<init>(animal.java:29)
06-07 17:41:50.150: E/AndroidRuntime(624):  at java.lang.Class.newInstanceImpl(Native Method)
06-07 17:41:50.150: E/AndroidRuntime(624):  at java.lang.Class.newInstance(Class.java:1319)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
06-07 17:41:50.150: E/AndroidRuntime(624):  ... 11 more


我查了一下这个问题,似乎它与清单有关,没有正确地声明活动,但由于清单以前工作正常,所以我没有弄乱。即使经过仔细检查,我的清单也很好。我不知道是什么导致了此错误以及是什么导致了我的应用崩溃。我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dapps.factpalooza"
    android:versionCode="1"
    android:versionName="1.0" >

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

        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true">

        <activity
        android:name="com.google.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">

        </activity>

        <activity
            android:name="com.dapps.factpalooza.StartMenu"
            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="com.dapps.factpalooza.animal"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.dapps.factpalooza.Food"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.dapps.factpalooza.World"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.dapps.factpalooza.Law"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.dapps.factpalooza.Random_facts"
            android:label="@string/app_name" >

        </activity>
    </application>

</manifest>


我的菜单代码:

package com.dapps.factpalooza;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class StartMenu extends ListActivity {

    String menuItems[] = {"Animal Facts", "Food Facts", "World Facts","Random Facts","Crazy Laws"};
    String classNames[] = {"animal","Food","World","Random_facts","Law"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(StartMenu.this, android.R.layout.simple_list_item_1, menuItems));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String classSelect = classNames[position];

        try{
            Class select = Class.forName("com.dapps.factpalooza." + classSelect);
            Intent selection = new Intent(StartMenu.this, select);
            startActivity(selection);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }



}


以及列表中我的页面之一中的代码:

package com.dapps.factpalooza;

import java.util.Random;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class animal extends Activity {
    Button Generate;
    TextView Fact;
    TextView factNum;
    String animal_facts[] = {
            "Fact1",
            "Fact2",
            "Fact3",
            "Fact4",
            "Fact5",
            "Fact6",
            };
    int total_facts = 5;
    Random r = new Random();
    int Choosen;
    AdView ad = (AdView) findViewById(R.id.ad);

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

        setContentView(R.layout.main);

        Generate = (Button) findViewById(R.id.generate);
        Fact = (TextView) findViewById(R.id.fact_display);
        factNum = (TextView) findViewById(R.id.fact_number);



        Generate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ad.loadAd(new AdRequest());
                Choosen = r.nextInt(total_facts);
                Fact.setText(animal_facts[Choosen]);
                factNum.setText("Fact # "+ Choosen);

            }
        });
    }



}

最佳答案

我查了一下这个问题,似乎与我的清单有关


没有。

06-07 17:41:50.150: E/AndroidRuntime(624): Caused by: java.lang.NullPointerException
06-07 17:41:50.150: E/AndroidRuntime(624):  at android.app.Activity.findViewById(Activity.java:1825)
06-07 17:41:50.150: E/AndroidRuntime(624):  at com.dapps.factpalooza.animal.<init>(animal.java:29)


您试图从数据成员的初始化程序调用findViewFromId()。您只能在调用findViewById()或以其他方式建立内容视图之后调用setContentView()-否则,将找不到任何小部件。

09-28 02:45
查看更多