1

我真的是 Android 编程的新手,我的代码有任何错误,如果这段代码有问题,有人可以帮忙吗?日志猫向我显示了这些错误:

08-07 11:59:46.773: E/AndroidRuntime(26185): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{uiv.makedirect.activity/uiv.makedirect.activity.ManagerActivity}: java.lang.NullPointerException
08-07 11:59:46.773: E/AndroidRuntime(26185):    at uiv.makedirect.utilities.RadioStateDrawable.<init>(RadioStateDrawable.java:78)
08-07 11:59:46.773: E/AndroidRuntime(26185):    at uiv.makedirect.utilities.TabBarButton.setState(TabBarButton.java:66)

我的代码:我用于创建 Tabactivity 的活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*
     * set this activity as the tab bar delegate so that onTabChanged is called when users tap on the bar
     */
    setDelegate(new SliderBarActivityDelegateImpl());

    Intent home = new Intent(this, HomeActivity.class);
    Intent feed = new Intent(this, FeedActivity.class);
    Intent cart = new Intent(this, CartActivity.class);
    Intent invite = new Intent(this, InviteActivity.class);
    Intent account = new Intent(this, AccountActivity.class);

    /*
     * This adds a title and an image to the tab bar button Image should be a PNG file with transparent background. Shades are opaque areas in on and off state are specific as parameters
     */
    this.addTab("Home", R.drawable.star, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_BLUE, home);
    this.addTab("Feed", R.drawable.icon_feed, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_BLUE, feed);
    this.addTab("Cart", R.drawable.ico_cart, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_BLUE, cart);
    this.addTab("Invite", R.drawable.ico_invite, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_BLUE, invite);
    this.addTab("Account", R.drawable.star, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_BLUE, account);

    /*
     * commit is required to redraw the bar after add tabs are added if you know of a better way, drop me your suggestion please.
     */
    commit();
}

ScrollableTabActivity:

public class ScrollableTabActivity extends ActivityGroup  implements RadioGroup.OnCheckedChangeListener{

private LocalActivityManager activityManager;
private LinearLayout contentViewLayout;
private LinearLayout.LayoutParams contentViewLayoutParams;
private HorizontalScrollView bottomBar;
private RadioGroup.LayoutParams buttonLayoutParams;
private RadioGroup bottomRadioGroup;
private Context context;
private List intentList;
private List titleList;
private List states;
private SliderBarActivityDelegate delegate;
private int defaultOffShade;
private int defaultOnShade;

private IntentFilter changeTabIntentFilter;
private ChangeTabBroadcastReceiver changeTabBroadcastReceiver;
public static String CURRENT_TAB_INDEX;
public static String ACTION_CHANGE_TAB = "com.mobyfactory.changeTab";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;

    activityManager = getLocalActivityManager();
    setContentView(R.layout.customslidingtabhost);
    contentViewLayout = (LinearLayout)findViewById(R.id.contentViewLayout);
    bottomBar = (HorizontalScrollView)findViewById(R.id.bottomBar);
    bottomRadioGroup = (RadioGroup)findViewById(R.id.bottomMenu);
    contentViewLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

    defaultOffShade = RadioStateDrawable.SHADE_GRAY;
    defaultOnShade = RadioStateDrawable.SHADE_YELLOW;

    bottomRadioGroup.setOnCheckedChangeListener(this);
    intentList  = new ArrayList();
    titleList   = new ArrayList();
    states      = new ArrayList();

    buttonLayoutParams = new RadioGroup.LayoutParams(320/5, RadioGroup.LayoutParams.WRAP_CONTENT);
}

public void onResume()
{
    changeTabIntentFilter = new IntentFilter(ACTION_CHANGE_TAB);
    changeTabBroadcastReceiver = new ChangeTabBroadcastReceiver();
    registerReceiver(changeTabBroadcastReceiver, changeTabIntentFilter);
    super.onResume();
}

public void onPause()
{
    unregisterReceiver(changeTabBroadcastReceiver);
    super.onPause();
}

public void commit()
{
    bottomRadioGroup.removeAllViews();

    int optimum_visible_items_in_portrait_mode = 5;
    try
    {
        WindowManager window = getWindowManager();
        Display display = window.getDefaultDisplay();
        int window_width = display.getWidth();

        optimum_visible_items_in_portrait_mode = (int) (window_width/64.0);
    }
    catch (Exception e)
    {
        optimum_visible_items_in_portrait_mode = 5;
    }

    int screen_width = getWindowManager().getDefaultDisplay().getWidth();
    int width;
    if (intentList.size()<=optimum_visible_items_in_portrait_mode)
    {
        width = screen_width/intentList.size();
    }
    else
    {
        width = screen_width/5;
        //width = 320/5; 
    }
    RadioStateDrawable.width = width;
    RadioStateDrawable.screen_width = screen_width;
    buttonLayoutParams = new RadioGroup.LayoutParams(width, RadioGroup.LayoutParams.WRAP_CONTENT);

    for (int i=0; i<intentList.size(); i++)
    {
        TabBarButton tabButton = new TabBarButton(this);
        int[] iconStates = (int[]) states.get(i);
        if (iconStates.length==1)
            tabButton.setState( titleList.get(i).toString(),iconStates[0]);
        else if (iconStates.length==2)
            tabButton.setState(titleList.get(i).toString(), iconStates[0], iconStates[1]);
        else if (iconStates.length==3) 
            tabButton.setState(titleList.get(i).toString(), iconStates[0], iconStates[1], iconStates[2]);

        tabButton.setId(i);
        tabButton.setGravity(Gravity.CENTER);
        bottomRadioGroup.addView(tabButton, i, buttonLayoutParams);
    }

    setCurrentTab(0);
}

protected void addTab(String title, int offIconStateId, int onIconStateId, Intent intent)
{
    int[] iconStates = {onIconStateId, offIconStateId};
    states.add(iconStates);
    intentList.add(intent);
    titleList.add(title);
    //commit();
}

protected void addTab(String title, int iconStateId, Intent intent)
{
    //int[] iconStates = {iconStateId};
    int[] iconStates = {iconStateId, defaultOffShade, defaultOnShade};
    states.add(iconStates);
    intentList.add(intent);
    titleList.add(title);
    //commit();
}

protected void addTab(String title, int iconStateId, int offShade, int onShade, Intent intent)
{
    int[] iconStates = {iconStateId, offShade, onShade};
    states.add(iconStates);
    intentList.add(intent);
    titleList.add(title);
    //commit();
}

和 RadioStateDrawable 用于在选项卡中显示图像:

public class RadioStateDrawable extends Drawable {

private Bitmap bitmap;
private Bitmap highlightBitmap;
private Shader shader;
private Shader textShader;
Context context;
public static int width;
public static int screen_width;
private boolean highlight;
private String label;

public static final int SHADE_GRAY = 0;
public static final int SHADE_BLUE = 1;
public static final int SHADE_MAGENTA = 2;
public static final int SHADE_YELLOW = 3;
public static final int SHADE_GREEN = 4;
public static final int SHADE_RED = 5;
public static final int SHADE_ORANGE = 6;

public RadioStateDrawable(Context context, int imageID, String label, boolean highlight, int shade)
{
    super();
    this.highlight = highlight;
    this.context = context;
    this.label = label;
    InputStream is = context.getResources().openRawResource(imageID);
    bitmap = BitmapFactory.decodeStream(is).extractAlpha();
    setShade(shade);

    highlightBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bottom_bar_highlight);
}

public RadioStateDrawable(Context context, int imageID, String label, boolean highlight, int startGradientColor, int endGradientColor)
{
    super();
    this.highlight = highlight;
    this.context = context;
    this.label = label;
    InputStream is = context.getResources().openRawResource(imageID);
    bitmap = BitmapFactory.decodeStream(is).extractAlpha();
    int[] shades = new int[] { startGradientColor, endGradientColor };
    shader = new LinearGradient(0, 0, 0, bitmap.getHeight(), shades, null, Shader.TileMode.MIRROR);
}

public void setShade(int shade)
{
    int[] shades = new int[2];
    switch (shade)
    {
    case SHADE_GRAY: {
        shades = new int[] { Color.LTGRAY, Color.DKGRAY };
        break;
    }
    case SHADE_BLUE: {
        shades = new int[] { Color.CYAN, Color.BLUE };
        break;
    }
    case SHADE_RED: {
        shades = new int[] { Color.MAGENTA, Color.RED };
        break;
    }
    case SHADE_MAGENTA: {
        shades = new int[] { Color.MAGENTA, Color.rgb(292, 52, 100) };
        break;
    }
    case SHADE_YELLOW: {
        shades = new int[] { Color.YELLOW, Color.rgb(255, 126, 0) };
        break;
    }
    case SHADE_ORANGE: {
        shades = new int[] { Color.rgb(255, 126, 0), Color.rgb(255, 90, 0) };
        break;
    }
    case SHADE_GREEN: {
        shades = new int[] { Color.GREEN, Color.rgb(0, 79, 4) };
        break;
    }
    }

    shader = new LinearGradient(0, 0, 0, bitmap.getHeight(), shades, null, Shader.TileMode.MIRROR);

    if (highlight)
        textShader = new LinearGradient(0, 0, 0, 10, new int[] { Color.WHITE, Color.LTGRAY }, null, Shader.TileMode.MIRROR);
    else
        textShader = new LinearGradient(0, 0, 0, 10, new int[] { Color.LTGRAY, Color.DKGRAY }, null, Shader.TileMode.MIRROR);
}

@Override
public void draw(Canvas canvas) {

    int bwidth = bitmap.getWidth();
    int bheight = bitmap.getHeight();
    /*
     * if (width==0) { if (screen_width==0) screen_width = 320; width=screen_width/5; }
     */
    int x = (width - bwidth) / 2;
    int y = 2;

    canvas.drawColor(Color.TRANSPARENT);
    Paint p = new Paint();

    p.setColor(Color.WHITE);
    p.setStyle(Paint.Style.FILL);
    p.setTextSize(19);
    p.setTypeface(Typeface.DEFAULT_BOLD);
    p.setFakeBoldText(true);
    p.setTextAlign(Align.CENTER);
    p.setShader(textShader);
    p.setAntiAlias(true);
    canvas.drawText(label, width / 2, y + bheight + 20, p);

    p.setShader(shader);
    canvas.drawBitmap(bitmap, x, y, p);
}

更改图像选项卡时出现错误

4

2 回答 2

0

我的猜测是,您的应用程序由于 RadioStateDrawable 函数中的最后一行而崩溃。

从您的资源中获取特定可绘制对象的方法是这样的:

    int id = getResources().getIdentifier(bottom_bar_highlight,"drawable", getPackageName());
    BitmapFactory.decodeResource(getResources(), id);
于 2012-08-07T05:39:33.797 回答
0

清楚地阅读您的代码后,我认为错误 java.lang.NullPointerException 可能来自context.getResources()。我用谷歌搜索了context.getResources().openRawResource()context.getResources().getIdentifier()的用法。

我认为您向此方法传递了错误的参数:context.getResources().getIdentifier()

你可以试试这个:

int id = context.getResources().getIdentifier("drawable/bottom_bar_highlight","drawable", context.getPackageName());

祝你好运。

于 2012-08-09T07:59:24.713 回答