选项卡式导航,将意图附加内容传递给片段

时间:2014-05-16 01:00:52

标签: java android android-intent android-fragments

快速提问。

我正在开发一款包含2项活动的应用。

活动A 使用带有3个标签的标签导航。每个标签都有一个片段,其中有几个以宿舍命名的按钮。每个按钮作为宿舍名称的ID,因此,点击宿舍A将启动活动B ,并使用案例陈述并找到按钮的ID将以EXTRA字符串启动活动B. ,宿舍的名称。

活动B 还使用标签导航,包含3个标签。一个列出人员,一个列出事件,一个列出RA的办公时间。

我的问题是启动活动并将Extra添加到活动中,这将根据Extra字符串添加来自数据库的信息(I.E.点击宿舍A将执行SQL SELECT WHERE Dorm ='EXTRA'或其他东西)。我尝试在这里使用教程:http://wptrafficanalyzer.in/blog/itemclick-handler-for-listfragment-in-android/但我的应用程序崩溃了。我已经在这里和那里使用了一些其他的教程,试图理解Fragments如何工作,我对Android还不是很熟悉。在不尝试通过EXTRA的情况下,我可以运行APP并使用一些填充文本或按钮或其他任何内容显示一些非常简单的视图。

我尝试使用这里的一些代码:Where/How to getIntent().getExtras() in an Android Fragment?但我真的不太明白它是如何工作的,当我使用它时我得到了相同的结果:崩溃。

这是LogCat,我可以看到,当我试图获取String意图时,NullPointerException会抛出错误:

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

活动A :(其中一些评论是我将要尝试并稍后修复的其他一些misc问题)

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;
        }
    }
}

活动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;
    }

}

试图获得额外的

的片段
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;
    }
}

编辑,我工作

我弄明白了什么。

我正在使用一种自定义TabListener类。我用过这个:

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

它就像一个魅力,所以现在我的活动B的代码,我刚刚添加了一个peopleTab.setTag(b);在我将它们添加到操作栏之前,然后在我的TabListener类中添加了

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

正如上面的链接所解释的那样。我想我只是看得太远了!我倾向于在蚂蚁山上建造山脉......但是感谢你的支持!

1 个答案:

答案 0 :(得分:0)

您还需要在活动上捆绑字符串。

Passing a bundle on startActivity

您是否检查过有值传递?您是否运行调试器以查看值是否正在传递?