代码不适用于Android 4.2,但适用于Android 2.3

代码不适用于Android 4.2,但适用于Android 2.3,android,version,Android,Version,我有一组代码在Android2.3上运行正常,但在4.2 emulator上运行不正常。这是一个滑动菜单代码,其中包含一组tabhost。滑出菜单代码已从下载。谁能告诉我我有什么问题吗 这是我的密码: import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.graphics.Color; i

我有一组代码在Android2.3上运行正常,但在4.2 emulator上运行不正常。这是一个滑动菜单代码,其中包含一组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"));

    }

    }
这里是:getDrawabl(…)
添加e,它应该可以工作


但是Eclipse应该已经显示了这一点。

由com.e_idglobal.SampleActivity.onCreate(SampleActivity.java:34)上的java.lang.NullPointerException引起。
onCreate的第34行有一个NPE@Benjanmin,第34行是if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){getActionBar().hide();}。我怀疑这种情况可能与Android版本有关。但不知道在哪里。感谢您将这一行拆分为两行-
if
getActionBar()…
,这样可能更容易找到错误-然后,将断点放在那里,看看它在哪一行崩溃,然后用stacktrace/logcat更新您的问题以及崩溃的那一行occurs@Benjamin,我已经按照你的建议重做了。我将.hide()放在另一行中。Logcat建议它在这里。看起来ActionBar无法隐藏。这在版本2.3中是可以的,因为它没有ActionBar。如果你想一直删除你的
ActionBar
,请看,这可能就是你需要的。对不起,我一定是无意中删除了e。它在我的月食中。
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
VerifyOthersspec.setIndicator("Verify Others", getResources().getDrawabl(R.drawable.ic_verify_other));