【问题标题】:code not working on Android 4.2 but ok on Android 2.3代码在 Android 4.2 上不起作用,但在 Android 2.3 上没问题
【发布时间】:2013-07-12 07:15:45
【问题描述】:

我有一组代码在 android 2.3 上运行正常,但在 4.2 模拟器上运行不正常。这是一个滑出式菜单代码,其中包含一组 tabhost。滑出菜单代码是从here 下载的。有人可以告诉我我的问题是什么。

这是我的代码:

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

    }

    }

logcat 在这里

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

【问题讨论】:

  • Caused by: java.lang.NullPointerException at com.e_idglobal.SampleActivity.onCreate(SampleActivity.java:34) onCreate的第34行有一个NPE
  • @Benjanmin,第 34 行是 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ getActionBar().hide(); }。我怀疑这可能与 Android 版本有关。但不知道在哪里。谢谢
  • 尝试将此行拆分为两行 - ifgetActionBar()... ,这样可能更容易找到错误 - 之后,在此处放置断点,然后查看在哪一行它崩溃了,然后用 stacktrace/logcat 和发生崩溃的行更新你的问题
  • @Benjamin,我按照你的建议重做了。我在单独的行中有 .hide() 。 Logcat 建议它在这里。看起来 ActionBar 无法隐藏。这在 2.3 版中没问题,因为它没有 ActionBar。那么我们该如何解决呢。
  • 如果您想一直删除您的ActionBar,请参阅here,这可能就是您所需要的

标签: android version


【解决方案1】:
VerifyOthersspec.setIndicator("Verify Others", getResources().getDrawabl(R.drawable.ic_verify_other));

这里是:getDrawabl(...);
添加 e 就可以了。

这应该已经被 Eclipse 显示出来了。

【讨论】:

  • 对不起,我一定是不小心删除了 e。它在我的 Eclipse 中。
猜你喜欢
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2020-12-05
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
相关资源
最近更新 更多