我有一组代码在 android 2.3 上可以正常工作,但在 4.2 模拟器上不行。这是一个滑出式菜单代码,其中包含一组 tabhost。滑出式菜单代码是从这里下载的。有人可以告诉我我的问题是什么。
这是我的代码:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.app.TabActivity;
import com.korovyansk.android.slideout.SlideoutActivity;
public class SampleActivity extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
@SuppressWarnings("deprecation")
@TargetApi(16)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
getActionBar().hide();
}
findViewById(R.id.sample_button).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) { int width = (int) TypedValue.applyDimension TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
SlideoutActivity.prepare (SampleActivity.this, R.id.inner_content, width);
startActivity(new Intent(SampleActivity.this,MenuActivity.class));
overridePendingTransition(0, 0);
}
});
TabHost tabHost = getTabHost();
// Tab for ID
TabSpec IDspec = tabHost.newTabSpec("ID");
IDspec.setIndicator("My e-ID", getResources().getDrawable(R.drawable.ic_id));
Intent MyEidIntent = new Intent(this, MyEidActivity.class);
IDspec.setContent(MyEidIntent);
// Tab for Verify_Me
TabSpec VerifyMespec = tabHost.newTabSpec("Verify_Me");
// setting Title and Icon for the Tab
VerifyMespec.setIndicator("Verify Me", getResources().getDrawable(R.drawable.ic_verify_me));
Intent VerifyMeIntent = new Intent(this, VerifyMeActivity.class);
VerifyMespec.setContent(VerifyMeIntent);
// Tab for Verify Other
TabSpec VerifyOthersspec = tabHost.newTabSpec("Verify_Others");
VerifyOthersspec.setIndicator("Verify Others", getResources().getDrawabl(R.drawable.ic_verify_other));
Intent VerifyOtherIntent = new Intent(this, VerifyOtherActivity.class);
VerifyOthersspec.setContent(VerifyOtherIntent);
// Tab for Scan
TabSpec Scanspec = tabHost.newTabSpec("Scan");
Scanspec.setIndicator("Scan", getResources().getDrawable(R.drawable.ic_verify_other));
Intent ScanspecIntent = new Intent(this, ScanActivity.class);
Scanspec.setContent(ScanspecIntent);
// Tab for Setting
TabSpec Settingspec = tabHost.newTabSpec("Setting");
Settingspec.setIndicator("Setting", getResources().getDrawable(R.drawable.ic_setting));
Intent SettingIntent = new Intent(this, SettingActivity.class);
Settingspec.setContent(SettingIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(IDspec);
tabHost.addTab(VerifyMespec); // Adding photos tab
tabHost.addTab(VerifyOthersspec); // Adding songs tab
tabHost.addTab(Scanspec); // Adding videos tab
tabHost.addTab(Settingspec);
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E0FFFF"));
TextView x = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
x.setTextSize(10);
x.setTextColor(Color.parseColor("#000000"));
}
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#adff87"));
TextView y = (TextView) tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.title);
y.setTextColor(Color.parseColor("#FF0000"));
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
TabHost tabHost= getTabHost();
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E0FFFF"));
TextView x = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
x.setTextColor(Color.parseColor("#000000"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor (Color.parseColor("#adff87"));
TextView y = (TextView) tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.title);
y.setTextColor(Color.parseColor("#FF0000"));
}
}
日志猫在这里
07-12 06:47:35.470: E/AndroidRuntime(1405): FATAL EXCEPTION: main
07-12 06:47:35.470: E/AndroidRuntime(1405): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.e_idglobal/com.e_idglobal.SampleActivity}: java.lang.NullPointerException
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.os.Looper.loop(Looper.java:137)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-12 06:47:35.470: E/AndroidRuntime(1405): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 06:47:35.470: E/AndroidRuntime(1405): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 06:47:35.470: E/AndroidRuntime(1405): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-12 06:47:35.470: E/AndroidRuntime(1405): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-12 06:47:35.470: E/AndroidRuntime(1405): at dalvik.system.NativeStart.main(Native Method)
07-12 06:47:35.470: E/AndroidRuntime(1405): Caused by: java.lang.NullPointerException
07-12 06:47:35.470: E/AndroidRuntime(1405): at com.e_idglobal.SampleActivity.onCreate(SampleActivity.java:34)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.Activity.performCreate(Activity.java:5104)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-12 06:47:35.470: E/AndroidRuntime(1405): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-12 06:47:35.470: E/AndroidRuntime(1405): ... 11 more