Reputation: 1722
I'm just starting to program Android Apps and I've been coding along with a book that I got. The app is just suppose to run a bunch of test activists from a list activity. I test the list activity and it worked fine. I also tried to test the first activity on the activity list and it just gets stuck trying to load it and will just keep trying to load it. My code is practically verbatim from the book besides the package name. I know that it finds the class, it's just the activity doesn't launch and I don't know why. I feel like I'm just missing something simple or overlooking a small error. Here are the three file I have for the app so far. I'm pretty sure it has to do with the intent in AndroidBasicStarter.java, but I'm including everything just in case it is not.
Edit: I'm having trouble loading LifeCycleTest.java. I did not code any more tests because the first one doesn't work and I don't know why.
AndroidBasicStarter.java
package com.dom.starter;
import android.os.Bundle;
import android.content.Intent;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AndroidBasicStarter extends ListActivity
{
String tests[] = { "LifeCycleTest", "SingleTouchTest", "MultiTouchTest",
"KeyTest", "AccelerometerTest", "AssestsTest", "ExternalStorageTest",
"SoundPoolTest", "MediaPlayerTest", "FullScreenTest", "RenderViewTest",
"ShapeTest", "BitmapTest", "FontTest", "SurfaceTest"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,tests));
}
@Override
protected void onListItemClick(ListView list, View view,
int position,long id)
{
super.onListItemClick(list, view, position, id);
String testName = tests[position];
try
{
Class clazz = Class.forName("com.dom.starter." + testName);
Intent intent = new Intent(AndroidBasicStarter.this,clazz);
startActivity(intent);//problem here maybe?
}//end try
catch(ClassNotFoundException e)
{
e.printStackTrace();
}//end catch
}
}
LifeCycleTest.java
package com.dom.starter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LifeCycleTest extends Activity
{
StringBuilder builder = new StringBuilder();
TextView textView;
private void log(String text)
{
Log.d("LifeCycleTest", text);
builder.append(text);
builder.append('\n');
textView.setText(builder.toString());
}
@Override
public void onCreate(Bundle saveInstanceState)
{
super.onSaveInstanceState(saveInstanceState);
textView = new TextView(this);
textView.setText(builder.toString());
setContentView(textView);
log("Created");
}
@Override
protected void onResume()
{
super.onResume();
log("Resumed");
}
@Override
protected void onPause()
{
super.onPause();
log("Paused");
if(isFinishing())
log("Finishing");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dom.starter"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="AndroidBasicStarter"
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="LifeCycleTest"
android:label="Life Cycle Test"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
Logcat
10-10 12:50:39.807: E/AndroidRuntime(31182): FATAL EXCEPTION: main
10-10 12:50:39.807: E/AndroidRuntime(31182): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dom.starter/com.dom.starter.LifeCycleTest}: java.lang.NullPointerException
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.os.Looper.loop(Looper.java:137)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread.main(ActivityThread.java:4950)
10-10 12:50:39.807: E/AndroidRuntime(31182): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:50:39.807: E/AndroidRuntime(31182): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 12:50:39.807: E/AndroidRuntime(31182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-10 12:50:39.807: E/AndroidRuntime(31182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-10 12:50:39.807: E/AndroidRuntime(31182): at dalvik.system.NativeStart.main(Native Method)
10-10 12:50:39.807: E/AndroidRuntime(31182): Caused by: java.lang.NullPointerException
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.Activity.onSaveInstanceState(Activity.java:1222)
10-10 12:50:39.807: E/AndroidRuntime(31182): at com.dom.starter.LifeCycleTest.onCreate(LifeCycleTest.java:26)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.Activity.performCreate(Activity.java:5179)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-10 12:50:39.807: E/AndroidRuntime(31182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
10-10 12:50:39.807: E/AndroidRuntime(31182): ... 11 more
Thanks in advance.
Upvotes: 1
Views: 1833
Reputation: 2701
in onCreate in LifeCycleTest.java it should be super.onCreate(saveInstanceState);
instead of super.onSaveInstanceState(saveInstanceState);
Upvotes: 2
Reputation: 15382
As per your code only LifeCycleTest
will work, you need to add all the class in your Manifest.
change setContentView(textView);
setContentView(R.layout.life_cycle_test);
Upvotes: 0