Reputation: 659
Hey guys I was running my app in an emulator but I ran into an error that caused the application to crash whenever I selected a list item. The error log I got was:
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
I've looked up this problem and it seemed like it had something to do with my manifest and not declaring activities properly, but I haven't messed with my manifest since it was working properly before. Even after a double check my manifest looked fine. I have no idea what's causing this error and what's causing my app to crash. My manifest:
<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>
My code for the menu:
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();
}
}
}
and the code from one of my pages from the list:
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);
}
});
}
}
Upvotes: 0
Views: 217
Reputation: 1006664
I've looked up this problem and it seemed like it had something to do with my manifest
No.
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)
You are trying to call findViewFromId()
from an initializer of a data member. You can only call findViewById()
after you have called setContentView()
or otherwise established the content view -- otherwise, there are no widgets to be found.
Upvotes: 4
Reputation: 6516
I think that problem is here
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)
You want to find view which doesn't exist.
[UPDATE]
I agree with CommonsWare. I didn't notice that you don't call setContentView()
method.
Upvotes: 1