无法实例化活动ComponentInfo错误(Checked Manifest)

时间:2013-06-08 18:41:28

标签: java android android-intent android-manifest

嘿伙计我在模拟器中运行我的应用程序但是我遇到了一个错误,导致应用程序在我选择列表项时崩溃。我得到的错误日志是:

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

            }
        });
    }



}

2 个答案:

答案 0 :(得分:4)

  

我已经查出了这个问题,似乎它与我的清单有关

没有

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() 或以其他方式建立内容视图 - 否则,找不到小部件。

答案 1 :(得分:1)

我认为问题就在这里

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)

您想要查找不存在的视图。

<强> [UPDATE] 我同意CommonsWare。我没有注意到你没有调用setContentView()方法。