我有一个主要活动和 3 个子活动。我可以很好地导航到其中的 2 个,当我尝试转到第 3 个时,我的应用程序崩溃了。这是启动崩溃活动的代码:
in = new Intent(this, BarPage.class);
in.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(in);
break;
这是我开始的活动的代码:
public class BarPage extends Activity {
public static Context appContext;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_page_lo);
// Show the Up button in the action bar.
setupActionBar();
appContext = getApplicationContext();
TextView tv= (TextView)findViewById(R.id.tv_bar_name);
InputStream ins;
byte[] bytes;
String string;
try {
ins = appContext.getResources().getAssets().open("bar-data.json");
bytes = new byte[ins.available()];
ins.read(bytes);
string = new String(bytes);
JSONObject jsonobj = new JSONObject(string);
tv.setText(jsonobj.getString("name"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我开始的活动的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BarPage" >
<TextView
android:id="@+id/tv_bar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
这是我的错误:
08-10 21:06:59.922: W/dalvikvm(3941): VFY: unable to resolve static method 9: Landroid/support/v4/app/NavUtils;.navigateUpFromSameTask (Landroid/app/Activity;)V
08-10 21:06:59.922: D/dalvikvm(3941): VFY: replacing opcode 0x71 at 0x000c
08-10 21:06:59.973: D/AndroidRuntime(3941): Shutting down VM
08-10 21:06:59.992: W/dalvikvm(3941): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-10 21:07:00.202: E/AndroidRuntime(3941): FATAL EXCEPTION: main
08-10 21:07:00.202: E/AndroidRuntime(3941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hawaiibarbook/com.hawaiibarbook.BarPage}: java.lang.NullPointerException
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.os.Looper.loop(Looper.java:137)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-10 21:07:00.202: E/AndroidRuntime(3941): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 21:07:00.202: E/AndroidRuntime(3941): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 21:07:00.202: E/AndroidRuntime(3941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-10 21:07:00.202: E/AndroidRuntime(3941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-10 21:07:00.202: E/AndroidRuntime(3941): at dalvik.system.NativeStart.main(Native Method)
08-10 21:07:00.202: E/AndroidRuntime(3941): Caused by: java.lang.NullPointerException
08-10 21:07:00.202: E/AndroidRuntime(3941): at com.hawaiibarbook.BarPage.setupActionBar(BarPage.java:59)
08-10 21:07:00.202: E/AndroidRuntime(3941): at com.hawaiibarbook.BarPage.onCreate(BarPage.java:28)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.Activity.performCreate(Activity.java:5104)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-10 21:07:00.202: E/AndroidRuntime(3941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-10 21:07:00.202: E/AndroidRuntime(3941): ... 11 more