我正在尝试使用片段,并在MainActivity中的方法中启动一个活动。我的代码MainActiviy.java是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up toolbar here
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setTitle(" Daily Qura'n Verses");
setSupportActionBar(toolbar);
// add tabs in the mainactivity
super.onPostResume();
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Today's Verse"),
TodayVerse.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Today's Chapter"),
TodayChapter.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Complete Qur'an"),
completeQuran.class, null);
// set Alarm for notification creation
createNotification();
}
其中的方法是:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_bar:
startActivity(new Intent(this, SearchResultsActivity.class));
default:
return super.onOptionsItemSelected(item);
}
}
但是,当我点击search_bar时,我收到错误。请帮我解决这个问题。
编辑1:
Log cat是:
8435-8435/com.example.shiza.dailyquranverses E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shiza.dailyquranverses/com.example.shiza.dailyquranverses.SearchResultsActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.shiza.dailyquranverses.SearchResultsActivity.onCreate(SearchResultsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
com.example.shiza.dailyquranverses.SearchResultsActivity.onCreate(SearchResultsActivity.java
此行显示您的搜索值存在一些问题,导致空指针异常,请检查您的值