gpt4 book ai didi

java - 无法实例化 Activity ComponentInfo 错误(已检查 list )

转载 作者:行者123 更新时间:2023-12-01 17:23:17 25 4
gpt4 key购买 nike

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

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

我查过这个问题,它似乎与我的 list 有关,并且没有正确声明 Activity ,但我没有弄乱我的 list ,因为它之前工作正常。即使经过仔细检查,我的 list 看起来也很好。我不知道是什么导致了这个错误以及是什么导致了我的应用程序崩溃。我的 list :

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

}
});
}



}

最佳答案

I've looked up this problem and it seemed like it had something to do with my manifest

没有。

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()。您只能在调用 setContentView() 或以其他方式建立内容 View 之后调用 findViewById() - 否则,没有任何小部件可以使用被发现。

关于java - 无法实例化 Activity ComponentInfo 错误(已检查 list ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com