简体   繁体   English

代码在Android 4.2上不起作用,但在Android 2.3上可以

[英]code not working on Android 4.2 but ok on Android 2.3

I have a set of code that is working ok on android 2.3 but not on 4.2 emulator. 我有一组代码可以在android 2.3上正常运行,但不能在4.2模拟器上运行。 This is a slideout menu code with a set of tabhost in it. 这是一个滑出菜单代码,其中包含一组tabhost。 The slideout menu code was downloaded from here . 此处下载了滑出菜单代码。 Can somebody please tell me what is my problem. 有人可以告诉我我的问题是什么。

Here is my code: 这是我的代码:

    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"));

    }

    }

The logcat is here 日志猫在这里

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));

Here it is: getDrawabl(...); 它是:getDrawabl(...);
Add the e and it should work. 添加e ,它应该可以工作。

This should have been shown by Eclipse though. Eclipse应该已经证明了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM