0

I have a custom object that I am constructing:

    package com.nanospark.upcdemo;

import android.os.Parcel;
import android.os.Parcelable;

public class listControlObject implements Parcelable {
    private String name;
    private int id;
    private int tempType;// 0 for fahrenheit, 1 for celsius.
    private int powerPercent;
    private int freq;
    private int temp;
    private int cycleDateDay;
    private int cycleDateMonth;

    private int numberOfCycles;
    private int cycleTimeHour;
    private int cycleTimeMinute;

    public listControlObject(String name, int id, int tempType,
            int powerPercent, int freq, int temp, int cycleTimeHour,
            int cycleTimeMinute, int cycleDateDay, int cycleDateMonth,
            int numberOfCycles) {
        this.name = name;
        this.id = id;
        this.tempType = tempType;
        this.powerPercent = powerPercent;
        this.freq = freq;
        this.temp = temp;
        this.cycleTimeMinute = cycleTimeMinute;
        this.cycleTimeHour = cycleTimeHour;
        this.cycleDateMonth = cycleDateMonth;
        this.cycleDateDay = cycleDateDay;
        this.numberOfCycles = numberOfCycles;
    }

    public int getCycleDateDay() {
        return cycleDateDay;
    }

    public void setCycleDateDay(int cycleDateDay) {
        this.cycleDateDay = cycleDateDay;
    }

    public int getCycleDateMonth() {
        return cycleDateMonth;
    }

    public void setCycleDateMonth(int cycleDateMonth) {
        this.cycleDateMonth = cycleDateMonth;
    }

    public int getCycleTimeHour() {
        return cycleTimeHour;
    }

    public void setCycleTimeHour(int cycleTimeHour) {
        this.cycleTimeHour = cycleTimeHour;
    }

    public int getCycleTimeMinute() {
        return cycleTimeMinute;
    }

    public void setCycleTimeMinute(int cycleTimeMinute) {
        this.cycleTimeMinute = cycleTimeMinute;
    }

    public int getNumberOfCycles() {
        return numberOfCycles;
    }

    public void setNumberOfCycles(int numberOfCycles) {
        this.numberOfCycles = numberOfCycles;
    }

    public int getTempType() {
        return tempType;
    }

    public void setTempType(int tempType) {
        this.tempType = tempType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPowerPercent() {
        return powerPercent;
    }

    public void setPowerPercent(int powerPercent) {
        this.powerPercent = powerPercent;
    }

    public int getFreq() {
        return freq;
    }

    public void setFreq(int freq) {
        this.freq = freq;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {

        return this.name;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
}

I also have two activities. All of the parameters for the object are taken from a second activity as described with the second activity's submission button here:

okButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(listaddactivity.this,
                        MainActivity.class);
                myIntent.putExtra(
                        "custom object",
                        new listControlObject(namefield.getText().toString(),
                                iterator, temperatureFrag.temperatureType
                                        .getCheckedRadioButtonId(), Integer
                                        .valueOf(powerPercent.getText()
                                                .toString()), Integer
                                        .valueOf(frequencyField.getText()
                                                .toString()),
                                temperatureFrag.temperaturenumberpicker
                                        .getValue(), cycleFrag.startTimeField
                                        .getCurrentHour(),
                                cycleFrag.startTimeField.getCurrentMinute(),
                                cycleFrag.cycleDatePicker.getDayOfMonth(),
                                cycleFrag.cycleDatePicker.getMonth(), Integer
                                        .valueOf(cycleFrag.numberOfCyclesField
                                                .getText().toString()))); // Optional
                                                                            // parameters
                listaddactivity.this.startActivity(myIntent);
                iterator++;  
            }

        });

My problem is; when I pass the intent to go back to the first activity; within the onCreate of the first (MainActivity) I have this code:

 public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {
    String TAG;


    HomeFragment homeFragment;
    cycleviewfragment cyclefragment;
    SectionsPagerAdapter mSectionsPagerAdapter;

    CustomViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        homeFragment = new HomeFragment();
        cyclefragment = new cycleviewfragment();
        cyclefragment.a1.add((listControlObject) savedInstanceState.get("custom object"));
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        mViewPager = (CustomViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setPagingEnabled(false);
        mViewPager
                .setOnPageChangeListener(new CustomViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }


    }

Where this line is causing a logcat error:

cyclefragment.a1.add((listControlObject) savedInstanceState.get("custom object"));

Where a1 is an arraylist of type listControlObject which is adapted to the ListFragment. (The cyclefragment class implements ListFragment).

Logcat error:

  03-23 20:57:03.304: E/AndroidRuntime(2303): FATAL EXCEPTION: main
03-23 20:57:03.304: E/AndroidRuntime(2303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.upcdemo/com.nanospark.upcdemo.MainActivity}: java.lang.NullPointerException
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.os.Looper.loop(Looper.java:153)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread.main(ActivityThread.java:4987)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at dalvik.system.NativeStart.main(Native Method)
03-23 20:57:03.304: E/AndroidRuntime(2303): Caused by: java.lang.NullPointerException
03-23 20:57:03.304: E/AndroidRuntime(2303):     at com.nanospark.upcdemo.MainActivity.onCreate(MainActivity.java:39)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.Activity.performCreate(Activity.java:5020)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-23 20:57:03.304: E/AndroidRuntime(2303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
03-23 20:57:03.304: E/AndroidRuntime(2303):     ... 11 more

Now - I am assuming this error is being called because there exists no "custom object" yet before the listaddactivity is called and happens from the user input and constructs the "custom object". Thus this will cause my nullpointer error.

My question is: Is there any way to refresh the FragmentList from the MainActivity when I construct a new object and send it through the intent with my listaddactivity back to my MainActivity? I.E. is there a way to update my FragmentList which is controlled by my MainActivity only after my listaddactivity happens.

Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64

1 Answers1

1

The savedInstanceState bundle is not the intent that you created when starting the activity.

Try this instead:

@Override
public void onCreate(Bundle savedInstanceState) {
    // do onCreate stuff
    processNewIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    processNewIntent(intent);
}

private void processNewIntent(Intent intent) {
    if (intent.hasExtra("custom_object")) {
        cyclefragment.a1.add((listControlObject) intent.getParcelableExtra("custom object"));
    }
}

When you want to send a new intent to your already-running second activity, you can add the Intent.FLAG_ACTIVITY_SINGLE_TOP so that the activity will not be re-created. Instead it will call onNewIntent().

mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
  • Tried implementing that; however I get "the method get(String) is undefined for the type Intent"; I'm guessing I have to construct a bundle in my listaddactivity instead of constructing an intent? and then pass the bundle instead? – Timothy Frisch Mar 24 '14 at 02:26
  • My bad, intent.getParcelableExtra() is the method you want. Answer edited. – mikejonesguy Mar 24 '14 at 02:37
  • Hey @mikejonesguy; I have the parcelable passing correctly however now my FragmentList in my MainActivity is not repopulating; even after I invalidate() it. – Timothy Frisch Mar 24 '14 at 03:34
  • Are you calling notifyDataSetChanged() on your adapter? – mikejonesguy Mar 24 '14 at 15:51
  • Check out this latest problem. http://stackoverflow.com/questions/22605321/calling-notifydatasetchanged-on-my-adapter-that-is-within-a-listfragment-cause – Timothy Frisch Mar 24 '14 at 21:25