我差不多两个小时寻找错误,也许你可以帮助我。我想改变 我创建的标签的TextColor。一切正常,它改变了颜色。 现在我想通过OnTabChanged更改颜色,但是程序崩溃时出现NullPointer异常。我看不出这个错误可以帮助我吗
Zakah.java
public class Zakah extends ActionBarActivity implements OnTabChangeListener{
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zakah);
TabHost tabHost=(TabHost) (findViewById(android.R.id.tabhost));
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Einleitung");
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setIndicator("Lehrmaterial");
spec2.setContent(R.id.tab2);
TabSpec spec3=tabHost.newTabSpec("Tab 3");
spec3.setIndicator("Tools");
spec3.setContent(R.id.tab3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
}
tabHost.setOnTabChangedListener(this);
//Spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.waehrung, R.layout.spinner_string);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
.
.
.
.
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C35817"));
}
控制台中的错误:
threadid = 1:线程退出,未捕获异常(group = 0x4184f898) 致命异常:主要是java.lang.NullPointerException eu.blackstone.muslimpocket.Zakah.onTabChanged(Zakah.java:96)at at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:463)at android.widget.TabHost.setCurrentTab(TabHost.java:448)at android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java:161)at at android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:563) 在android.view.View.performClick(View.java:4475)at android.view.View $ PerformClick.run(View.java:18784)at android.os.Handler.handleCallback(Handler.java:730)at android.os.Handler.dispatchMessage(Handler.java:92)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:5414)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:525)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)at dalvik.system.NativeStart.main(原生方法)
有什么想法吗?
答案 0 :(得分:0)
我的猜测是
tabHost.getCurrentTab()
返回-1所以
getChildAt(tabHost.getCurrentTab())
返回null。添加测试以避免currentTab为-1可以解决此问题。