不确定该怎么想这个问题。我为我的标签设置创建的这个方法名为tabSetUp()
,我之前创建过,并使用我的onCreate
方法调用,从那时起它已成功运行。现在,我突然在我的主要活动的另一个区域添加代码,现在我收到应用程序崩溃,说这是由我的选项卡设置方法引起的。没有意义。我错过了什么吗?我添加的新代码没有错误,但确实有黄色警告,说可能有空指针异常(我不太了解)。为什么我的应用程序崩溃了?谢谢你的帮助。 LogCat在下面。
MainSelectorActivity.java
package com.azurespot.disastertimer.app;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.Button;
import android.widget.TabWidget;
import com.azurespot.disastertimer.app.tabs.GodzillaTab;
import com.azurespot.disastertimer.app.tabs.NuclearTab;
import com.azurespot.disastertimer.app.tabs.TsunamiTab;
import com.azurespot.disastertimer.app.tabs.VolcanoTab;
import com.azurespot.disastertimer.app.tabs.ZombieTab;
import com.azurespot.disastertimer.app.themedactivities.GodzillaThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.NuclearThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.TsunamiThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.VolcanoThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity;
public class MainSelectorActivity extends FragmentActivity {
Resources resrc;
FragmentTabHost tabHost;
private Button btnStart;
public static final String TABTIMER = "Tab_and_Timer";
private String selectedTab;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_selector);
tabSetUp();
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToThemedActivity();
}
});
tabListener();
changeTabIndicators();
tabAndTimerPersist();
}
public void tabSetUp() {
resrc = getResources();
// TabHost setup & functionality
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//------Zombie tab------
//Creates tab and sets zombie icon in tab
tabHost.addTab(tabHost.newTabSpec("zombie").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_zombie_selected)),
ZombieTab.class, null);
//------Nuclear tab------
//Creates tab and sets nuclear icon in tab
tabHost.addTab(tabHost.newTabSpec("nuclear").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_nuclear_selected)),
NuclearTab.class, null);
//------Tsunami tab------
//Creates tab and sets tsunami icon in tab
tabHost.addTab(tabHost.newTabSpec("tsunami").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_tsunami_selected)),
TsunamiTab.class, null);
//------Godzilla tab------
//Creates tab and sets tsunami icon in tab
tabHost.addTab(tabHost.newTabSpec("godzilla").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_godzilla_selected)),
GodzillaTab.class, null);
//------Volcano tab------
//Creates tab and sets volcano icon in tab
tabHost.addTab(tabHost.newTabSpec("volcano").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_volcano_selected)),
VolcanoTab.class, null);
//set Zombie tab as default (zero based)
tabHost.setCurrentTab(0);
}
// Sets up the tabs to return a value, based on which one is currently selected.
// The int values will be used in the Start button's onClick to go to the corresponding activity.
public void tabListener() {
tabHost.setOnTabChangedListener(new FragmentTabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
selectedTab = tabId;
}
});
}
public void changeTabIndicators() {
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(R.id.tabhost);
TabWidget widget = tabHost.getTabWidget();
for(int i = 0; i < widget.getTabCount(); i++) {
View v = widget.getChildTabViewAt(i);
// // Look for the title view to ensure this is an indicator and not a divider.
// TextView tv = (TextView)v.findViewById(android.R.id.title);
// if(tv == null) {
// continue;
// }
v.setBackgroundResource(R.drawable.tab_selector_color);
}
}
public void goToThemedActivity() {
if (selectedTab.equals("zombie")) {
Intent i = new Intent(MainSelectorActivity.this, ZombieThemedActivity.class);
startActivity(i);
} else if (selectedTab.equals("nuclear")) {
Intent j = new Intent(MainSelectorActivity.this, NuclearThemedActivity.class);
startActivity(j);
} else if (selectedTab.equals("tsunami")) {
Intent k = new Intent(MainSelectorActivity.this, TsunamiThemedActivity.class);
startActivity(k);
} else if (selectedTab.equals("godzilla")) {
Intent l = new Intent(MainSelectorActivity.this, GodzillaThemedActivity.class);
startActivity(l);
} else if (selectedTab.equals("volcano")) {
Intent m = new Intent(MainSelectorActivity.this, VolcanoThemedActivity.class);
startActivity(m);
}
}
@Override
protected void onPause() {
super.onPause();
tabAndTimerPersist();
}
private void tabAndTimerPersist() {
SharedPreferences prefs = getSharedPreferences(TABTIMER, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("zombie", 0);
editor.putInt("nuclear", 0);
editor.putInt("tsunami", 0);
editor.putInt("godzilla", 0);
editor.putInt("volcano", 0);
editor.commit();
}
}
logcat的
1054-1054/com.azurespot.disastertimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.MainSelectorActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.azurespot.disastertimer.app.MainSelectorActivity.tabSetUp(MainSelectorActivity.java:60)
at com.azurespot.disastertimer.app.MainSelectorActivity.onCreate(MainSelectorActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
activity_main_selector.xml
<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:background="#7d8794"
tools:context="com.azurespot.disastertimer.app.MainSelectorActivity">
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:measureAllChildren="true"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:layout_marginTop="180dp"
android:gravity="center_horizontal">
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp" />
<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="0dp" />
<NumberPicker
android:id="@+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="0dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:gravity="center" >
<Button
android:id="@+id/btnStart"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ed872d"
android:text="@string/btn_start"
android:onClick="saveAndStartTimer"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:1)
从
更改此内容 tabHost = (android.support.v4.app.FragmentTabHost)findViewById(android.R.id.tabhost);
到
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);