Frage

Okay so this is what's going on in my Activity, fig 1---- It's iterate string array and creates buttons. chipContainer is a class (ChipMamma, shown below, fig 2), with an associated ViewGroup where chips are populated as buttons. That ChipMamma class is supposed to "manage" chips. The two commented-out lines were a previous implementation- with no ChipMamma, just the main activity managing the chips and populating buttons itself (which works). chipButton is a really simple extension of Button (also shown below, fig 3).

FIGURE 1

    String[] chipAr = {"one","two","three","four", "five","six","seven","eight", "nine","tententententententententententen","eleven","twelve"};
    int count = chipAr.length;

    for (int i = 0; i < count; i = i+1){
        chipContainer.newButton(chipAr[i]);
        //chipButton newButton = new chipButton(this, chipAr[i], i+1);
        //((FlowLayout) findViewById(R.id.chipContainer)).addView(newButton);
    }

When I use this class as a middle man, everything goes to poop. This is what I can't figure out. And the debug tells me that it can't find the source but I have the source SDK installed...

Figure 2

public class ChipMamma{ //This was first named "Chip Container"
    private Context context;
    private ArrayList<chipButton> chips;
    private ViewGroup vgContainer;
    private int count = 0;

    public ChipMamma(Context context, ViewGroup container){
            this.context = context;
            this.vgContainer = container;
    }
    public void newButton(String chip){
            ChipButton b = new ChipButton(this, this.context, chip, count+1);
            chips.add(b); //This could be problematic if you delete a count
            vgContainer.addView(chips.get(count+1));
            count += 1;
    }

This is a chip button class. It's constructor just sets the basic layout params of each chip button (which I would like to move to XML eventually). Basically the debugger gets to the end of the constructor and then gives me the error (even if I remove the last lines, I still get the same error. In fact, the only way I avoid the error is by removing all of the function calls in ChipMamma.newButton).

Figure 3

public class ChipButton extends Button {
final ChipMamma myMamma;
private Context context;
public ChipButton(ChipMamma theMamma, Context context, String chip, int id) {
        super(context);
        this.context = context;
        this.myMamma = theMamma;
                this.setId(id); //I've added extra indentation to paramater settings

                this.setPadding(10,  3,  10,  4); //needs to be XML

                //wiggleButton();

                this.setText(chip);

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
                    this.setBackground(this.getResources().getDrawable(R.drawable.button));
                } else{
                    this.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.button));
                }

                LayoutParams params = new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                ((Button)this).setLayoutParams(params);
}

Why can't I add these buttons from inside ChipMamma?

12-01 16:07:58.716: E/AndroidRuntime(6019): FATAL EXCEPTION: main
12-01 16:07:58.716: E/AndroidRuntime(6019): java.lang.RuntimeException: Unable to start activity ComponentInfo{nosite.spendstat/nosite.spendstat.Input}: java.lang.NullPointerException
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.os.Looper.loop(Looper.java:130)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread.main(ActivityThread.java:3806)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at java.lang.reflect.Method.invoke(Method.java:507)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at dalvik.system.NativeStart.main(Native Method)
12-01 16:07:58.716: E/AndroidRuntime(6019): Caused by: java.lang.NullPointerException
12-01 16:07:58.716: E/AndroidRuntime(6019):     at nosite.spendstat.ChipMamma.newButton(chipMamma.java:32)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at nosite.spendstat.Input.onCreate(Input.java:212)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-01 16:07:58.716: E/AndroidRuntime(6019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
12-01 16:07:58.716: E/AndroidRuntime(6019):     ... 11 more
War es hilfreich?

Lösung

Two things:

You need to instantiate the chips ArrayList before using it -- add

chips = new ArrayList<chipButton>()

to the ChipMamma constructor.

Then:

In ChipMamma.newButton() you are creating the new Button b and putting it into the ArrayList, but then calling addView and retrieving from the ArrayList at an out of bounds index (as ArrayList is 0 based). This causes the ArrayList to return null, and passes null to addView causing the crash.

To resolve, you should get the view out of the ArrayList at index count rather than (count + 1), or just pass b directly to addView.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top