我一直在浏览由newboston进行的android教程,并且在第41课时遇到了困难。我的应用程序在打开相机活动后立即关闭。
Camera.java
package com.thenewboston.akshit;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[] = {"StartingPoint", "TextPlay", "Email", "Camera"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
String cheese = classes[position];
super.onListItemClick(l, v, position, id);
try{
Class ourClass = Class.forName("com.thenewboston.akshit." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
Menu.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.akshit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.thenewboston.akshit.Splash"
android:label="Splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.StartingPoint"
android:label="Starting Point" >
<intent-filter>
<action android:name="com.thenewboston.akshit.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.Menu"
android:label="Starting Point" >
<intent-filter>
<action android:name="com.thenewboston.akshit.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.thenewboston.akshit.TextPlay"
android:label="Starting Point" >
</activity>
<activity
android:name="com.thenewboston.akshit.Email"
android:label="Starting Point" >
</activity>
<activity
android:name="com.thenewboston.akshit.Camera"
android:label="Starting Point" >
</activity>
</application>
}
的AndroidManifest.xml
06-28 03:23:03.080: D/dalvikvm(871): Not late-enabling CheckJNI (already on)
06-28 03:23:06.280: D/gralloc_goldfish(871): Emulator without GPU emulation detected.
06-28 03:23:09.520: D/dalvikvm(871): GC_FOR_ALLOC freed 55K, 5% free 3032K/3172K, paused 124ms, total 130ms
06-28 03:23:09.520: I/dalvikvm-heap(871): Grow heap (frag case) to 3.646MB for 635812-byte allocation
06-28 03:23:09.590: D/dalvikvm(871): GC_FOR_ALLOC freed 6K, 4% free 3646K/3796K, paused 63ms, total 63ms
06-28 03:23:10.210: I/Choreographer(871): Skipped 72 frames! The application may be doing too much work on its main thread.
06-28 03:23:15.810: D/AndroidRuntime(871): Shutting down VM
06-28 03:23:15.810: W/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0xb3a36b90)
06-28 03:23:15.850: E/AndroidRuntime(871): FATAL EXCEPTION: main
06-28 03:23:15.850: E/AndroidRuntime(871): Process: com.thenewboston.akshit, PID: 871
06-28 03:23:15.850: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.akshit/com.thenewboston.akshit.Camera}: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:137)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 03:23:15.850: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 03:23:15.850: E/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871): Caused by: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871): at com.thenewboston.akshit.Camera.initialize(Camera.java:37)
06-28 03:23:15.850: E/AndroidRuntime(871): at com.thenewboston.akshit.Camera.onCreate(Camera.java:29)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.Activity.performCreate(Activity.java:5243)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 03:23:15.850: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 03:23:15.850: E/AndroidRuntime(871): ... 11 more
06-28 03:23:19.850: I/Process(871): Sending signal. PID: 871 SIG: 9
日志
{{1}}
答案 0 :(得分:0)
在设置单击侦听器之前,必须在intialize函数中设置对ib的引用。
ib = (ImageButton) findViewById(R.id.ib) //Or the name that the ImageButton has
ib.setOnClickListener(this);