0

我正在为我的 android 应用程序使用 Sherlock 操作栏。但我注意到在 android Api 18 上我看不到操作栏。所以我决定在运行时自定义操作栏。但不幸的是,应用程序在 android api 18 模拟器上崩溃了。它一直在抱怨我知道存在的可绘制对象中的空指针。

   package com.example.rhemamedicare;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.rhemamedicare.R;

public class SteponeActivity extends SherlockActivity{


    Resources resources = null;
    Context context= null;
    @Override
    public void onCreate(Bundle savedInstanceState){


         ActionBar actionbar  =  null;
        context = SteponeActivity.this;
        resources = getResources();
        //This would enable the action bar to fall back to default action bar for android 4.0 and above to avoid errors
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

            this.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

            actionbar = this.getSupportActionBar();
            Drawable background = resources.getDrawable(R.drawable.ab_solid_jcodestyle);
            actionbar.setBackgroundDrawable(background);
        }
        else{
            actionbar = this.getSupportActionBar();
        }


        super.onCreate(savedInstanceState);
        setContentView(R.layout.steponeactivity);


        final Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
                // TODO Auto-generated method stub
                Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class);
                startActivity(intent);
                finish();
            }

        });
    }

}





    10-17 11:21:15.117: E/AndroidRuntime(861): FATAL EXCEPTION: main
10-17 11:21:15.117: E/AndroidRuntime(861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rhemamedicare/com.example.rhemamedicare.SteponeActivity}: java.lang.NullPointerException
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.os.Looper.loop(Looper.java:137)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-17 11:21:15.117: E/AndroidRuntime(861):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 11:21:15.117: E/AndroidRuntime(861):  at java.lang.reflect.Method.invoke(Method.java:511)
10-17 11:21:15.117: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 11:21:15.117: E/AndroidRuntime(861):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 11:21:15.117: E/AndroidRuntime(861):  at dalvik.system.NativeStart.main(Native Method)
10-17 11:21:15.117: E/AndroidRuntime(861): Caused by: java.lang.NullPointerException
10-17 11:21:15.117: E/AndroidRuntime(861):  at com.example.rhemamedicare.SteponeActivity.onCreate(SteponeActivity.java:35)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.Activity.performCreate(Activity.java:4465)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-17 11:21:15.117: E/AndroidRuntime(861):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-17 11:21:15.117: E/AndroidRuntime(861):  ... 11 more
10-17 11:22:03.366: I/Process(861): Sending signal. PID: 861 SIG: 9
4

2 回答 2

0

改变这个:

Drawable background= getApplicationContext().getResources().getDrawable(R.drawable.ab_solid_jcodestyle);

您需要应用程序的上下文来获取资源。

于 2013-10-17T11:39:09.400 回答
0

我注意到 xxhdpi 文件夹中没有可绘制对象。所以我在那里复制并粘贴了drawables并且应用程序工作了。

于 2013-10-17T12:58:01.520 回答