Pergunta

quick question.

I'm working on an app with 2 Activities.

Activity A uses Tab navigation with 3 tabs. Each tab holds a fragment, with a couple of buttons named after dormitories. Each button as an ID of the name of the dorm, so, clicking on Dorm A will launch Activity B, and using a case statement and finding the ID of the button will start Activity B with an EXTRA string, the name of the dorm.

Activity B also uses Tab navigation, with 3 tabs. One to list people, one to list events, and one to list office hours of RAs.

My problem is launching the activity and getting the Extra to the activity, which will add information from a database based on the Extra string (I.E. clicking on Dorm A will do an SQL SELECT WHERE Dorm = 'EXTRA' or something). I tried using a tutorial here: http://wptrafficanalyzer.in/blog/itemclick-handler-for-listfragment-in-android/ but my app has been crashing. I've used a few other tutorials here and there, trying to understand how Fragments work, I'm not very familiar with Android still. Without trying to pass the EXTRA, I can run the APP and display some very simple views with some filler text or buttons or whatever.

I tried using some code from here: Where/How to getIntent().getExtras() in an Android Fragment? but I don't really quite understand how it works, and when I used it I got the same results: crashing.

Here's the LogCat, which I can see that the error is thrown by a NullPointerException when I'm trying to get a String intent:

05-15 20:31:51.278: E/AndroidRuntime(2274): FATAL EXCEPTION: main 05-15 20:31:51.278: E/AndroidRuntime(2274): Process: ege493.test.hawksnesttabs, PID: 2274 05-15 20:31:51.278: E/AndroidRuntime(2274): java.lang.RuntimeException: Unable to start activity ComponentInfo{ege493.test.hawksnesttabs/ege493.test.hawksnesttabs.DormInfo}: java.lang.NullPointerException 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread.access$800(ActivityThread.java:135) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.os.Handler.dispatchMessage(Handler.java:102) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.os.Looper.loop(Looper.java:136) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread.main(ActivityThread.java:5017) 05-15 20:31:51.278: E/AndroidRuntime(2274): at java.lang.reflect.Method.invokeNative(Native Method) 05-15 20:31:51.278: E/AndroidRuntime(2274): at java.lang.reflect.Method.invoke(Method.java:515) 05-15 20:31:51.278: E/AndroidRuntime(2274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 05-15 20:31:51.278: E/AndroidRuntime(2274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-15 20:31:51.278: E/AndroidRuntime(2274): at dalvik.system.NativeStart.main(Native Method) 05-15 20:31:51.278: E/AndroidRuntime(2274): Caused by: java.lang.NullPointerException 05-15 20:31:51.278: E/AndroidRuntime(2274): at ege493.test.hawksnesttabs.Fragment_RA.onCreateView(Fragment_RA.java:33) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.Fragment.performCreateView(Fragment.java:1700) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.BackStackRecord.run(BackStackRecord.java:684) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.Activity.performStart(Activity.java:5240) 05-15 20:31:51.278: E/AndroidRuntime(2274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) 05-15 20:31:51.278: E/AndroidRuntime(2274): ... 11 more

Activity A: (some of the comments are some other misc problems that i'm going to try and fix later)

package ege493.test.hawksnesttabs;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    public final static String EXTRA_DORM = "ege493.Dorm";

    ActionBar actionBar;

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

        actionBar = getActionBar(); // Get reference to ActionBar

        // 1. Enable ActionBar navigation tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        // 2. Add the tabs
        Tab hasbrouckTab = actionBar.newTab();
        Tab parkerTab = actionBar.newTab();
        Tab southTab = actionBar.newTab();

        String label1 = getResources().getString(R.string.hasbrouck);
        hasbrouckTab.setText(label1).setTabListener(
                new TabListener<Fragment_Has>(this, R.id.quadParent,
                        Fragment_Has.class));

        String label2 = getResources().getString(R.string.parker);
        parkerTab.setText(label2).setTabListener(
                new TabListener<Fragment_Parker>(this, R.id.quadParent,
                        Fragment_Parker.class));

        String label3 = getResources().getString(R.string.south);
        southTab.setText(label3).setTabListener(
                new TabListener<Fragment_South>(this, R.id.quadParent,
                        Fragment_South.class));

        actionBar.addTab(hasbrouckTab);
        actionBar.addTab(parkerTab);
        actionBar.addTab(southTab);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "Acton settings selected", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
        return true;
    }

    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.buttonBevier:
            Intent i = new Intent(this, DormInfo.class);
            String dorm = "Bevier";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonCrispell:
            i = new Intent(this, DormInfo.class);
            dorm = "Crispell";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonDeyo:
            i = new Intent(this, DormInfo.class);
            dorm = "Deyo";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonDuBois:
            i = new Intent(this, DormInfo.class);
            dorm = "DuBois";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonLefevre:
            i = new Intent(this, DormInfo.class);
            dorm = "DuBois";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonBouton:
            i = new Intent(this, DormInfo.class);
            dorm = "Bouton";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;
        case R.id.buttonBliss:
            i = new Intent(this, DormInfo.class);
            dorm = "Bliss";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonCapen:
            i = new Intent(this, DormInfo.class);
            dorm = "Capen";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonCollango:
            i = new Intent(this, DormInfo.class);
            dorm = "College/Shango";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonGage:
            i = new Intent(this, DormInfo.class);
            dorm = "Gage";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonScudder:
            i = new Intent(this, DormInfo.class);
            dorm = "Scudder";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonEsopus:
            i = new Intent(this, DormInfo.class);
            dorm = "Esopus";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;

        case R.id.buttonLenape:
            i = new Intent(this, DormInfo.class);
            dorm = "Lenape";
            i.putExtra(EXTRA_DORM, dorm);
            startActivity(i);
            break;
        }
    }
}

Activity B

package ege493.test.hawksnesttabs;


import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class DormInfo extends Activity {
    ActionBar actionBar;

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

        // Set up the action bar.

        actionBar = getActionBar(); // Get reference to ActionBar

        //Using a different Action Bar, the tabs work well, but...
        //I'm trying to get rid of the title/icon/options menu. The Theme.Holo.NoActionBar
        //causes it to crash, and I can't get an 'onCreateOptionsMenu' to work.
        //Trying to allow a menu to work through hitting the menu button on the phone, but I do not want an
        //option for it in the action bar, or an action bar at all (which, I currently have no action bar)
        //Goal for this:::: Use the menu button to bring you back to dorm selection if you are already in the app

        // 1. Enable ActionBar navigation tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        // 2. Add the tabs
        Tab peopleTab = actionBar.newTab();
        Tab eventsTab = actionBar.newTab();
        Tab hoursTab = actionBar.newTab();

        String label1 = getResources().getString(R.string.ra_list);
        peopleTab.setText(label1).setTabListener(
                new TabListener<Fragment_RA>(this, R.id.dormParent,
                        Fragment_RA.class));

        String label2 = getResources().getString(R.string.dorm_events);
        eventsTab.setText(label2).setTabListener(
                new TabListener<Fragment_Events>(this, R.id.dormParent,
                        Fragment_Events.class));

        String label3 = getResources().getString(R.string.dorm_hours);
        hoursTab.setText(label3).setTabListener(
                new TabListener<Fragment_Hours>(this, R.id.dormParent,
                        Fragment_Hours.class));

        actionBar.addTab(peopleTab);
        actionBar.addTab(eventsTab);
        actionBar.addTab(hoursTab);

        //--------------------------------------------------------
        //Get the buttonID from the button that started the activity
        //--------------------------------------------------------

        /*Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_DORM);

        TextView tv = new TextView(this);
        tv.setTextSize(40);
        tv.setText(message);

        setContentView(tv);*/

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransacton = fragmentManager.beginTransaction();
        Fragment_RA detailsFragment = new Fragment_RA();
        //Fragment_Hours hoursFragment = new Fragment_Hours();
        //Fragment_Events eventsFragment = new Fragment_Events();
        Bundle b = new Bundle();
        b.putString(MainActivity.EXTRA_DORM, getIntent().getStringExtra(MainActivity.EXTRA_DORM));
        detailsFragment.setArguments(b);
        //fragmentTransacton.add(R.id.dormParent, detailsFragment);
        //hoursFragment.setArguments(b);
        //fragmentTransacton.add(R.id.dormParent, hoursFragment);
        //eventsFragment.setArguments(b);
        //fragmentTransacton.add(R.id.dormParent, eventsFragment);
        fragmentTransacton.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "Acton settings selected", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
        return true;
    }

}

The fragment that is trying to get the extra

package ege493.test.hawksnesttabs;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment_RA extends Fragment{

/*  public static Fragment_RA newInstance(int index) {
        Fragment_RA f = new Fragment_RA();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }
*/
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.people_fragment, null);
            TextView tv = (TextView) v.findViewById(R.id.tv);       
            Bundle b = getArguments();
            tv.setText("Details of " + b.getString(MainActivity.EXTRA_DORM));

            return v;
    }
}

EDIT, I GOT IT WORKING

I figured out what was up.

I'm using kind of a custom TabListener class. I used this:

how to initialize(pass arguments) fragments for action bar tabs?

And it worked like a charm, so now my code for Activity B, I just added a peopleTab.setTag(b); before I add them to the action bar, and then in my TabListener class I added

fragment = Fragment.instantiate(activity, fragmentClass.getName(), (Bundle) tab.getTag());

just as the link above explained. I guess I was just looking too far into this! I tend to make mountains out of ant hills... Thanks for the support, though!

Foi útil?

Solução

You need to bundle up the String on the Activity as well.

Passing a bundle on startActivity

Have you checked that there is a value getting passed too? Have you run the debugger to see if the value is being passed along?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top