0

我有两个片段,QuickNoteFragment 和 HistoryFragment,它们是 NavigationDrawer 的一部分(这是它们绑定到的 FragmentActivity)。在 QuickNoteFragment 我有一个按钮。按下此按钮时,我希望将另一个项目添加到 HistoryFragment 中的 ListView。

我问了这个问题:从另一个片段中更改 ListView 中的项目, 并从 srain 那里得到了一个很好的解决方案。但是当我这样做时,我得到了一个空指针异常。我们进行了很长时间(太长)的评论对话,试图找到解决方案,但无济于事。所以问题是:我如何摆脱这个异常?这就是我重现它的方式

打开应用程序 -> 它会自动启动 QuickNoteFragment。我按下按钮 -> 我通过 NavDrawer 打开 HistoryFragment(ViewPager 的一部分)。我没有得到带有字符串数据的吐司。-> 我回到 QuickNoteFragment -> 我按下按钮 -> 每次重复此操作时强制关闭。

这是NavigationDrawer我用来启动片段的:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Navigation_Drawer extends FragmentActivity {

    public DrawerLayout mDrawerLayout; // Creates a DrawerLayout called_.
    public ListView mDrawerList;
    public ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mNoterActivities; // This creates a string array called _.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

// Just setting up the navigation drawer

    } // End of onCreate

// Removed the menu


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) { 

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (position == 0) {
            Fragment qnfragment = new QuickNoteFragment(); 
            ((FragmentBase) qnfragment).setContext(this);
            Bundle args = new Bundle(); // Creates a bundle called args
            args.putInt(QuickNoteFragment.ARG_nOTERACTIVITY_NUMBER, position); 

            qnfragment.setArguments(args);

            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, qnfragment).commit();



        } else if (position == 3) {
            Fragment pendViewPager = new PendViewPager(); // This is a ViewPager that includes HistoryFragment
            ((FragmentBase) pendViewPager).setContext(this);
            Bundle args = new Bundle();

            pendViewPager.setArguments(args);
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, pendViewPager).commit();
        }

    // Update title etc
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) { // Used for the NavDrawer toggle
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) { // Used for the NavDrawer toggle
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

这是 QuickNoteFragment:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class QuickNoteFragment extends FragmentBase implements OnClickListener {

    public static final String ARG_nOTERACTIVITY_NUMBER = "noter_activity";

    Button b_create;
// removed other defining stuff

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.quicknote, container, false);
        int i = getArguments().getInt(ARG_nOTERACTIVITY_NUMBER);
        String noter_activity = getResources().getStringArray(
                R.array.noter_array)[i];
        FragmentManager fm = getFragmentManager();
        setRetainInstance(true);

        b_create = (Button) rootView.findViewById(R.id.qn_b_create);

        // Removed other stuff

        getActivity().setTitle(noter_activity);
        return rootView;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.qn_b_create:

                              String data = "String data";
                DataModel.getInstance().addItem(data);

            break;
        }
    }

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        }
    }

    public interface OnItemAddedHandler { // Srains code
        public void onItemAdded(Object data);
    }
}

这是 HistoryFragment (记住它是 a 的一部分ViewPager):

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.RiThBo.noter.QuickNoteFragment.OnItemAddedHandler;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryFragment extends FragmentBase implements OnItemAddedHandler {

    ListView lv;
    List<Map<String, String>> planetsList = new ArrayList<Map<String, String>>();
    SimpleAdapter simpleAdpt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.history, container, false);
        initList();


        ListView lv = (ListView) view.findViewById(R.id.listView);
        simpleAdpt = new SimpleAdapter(getActivity(), planetsList,
                android.R.layout.simple_list_item_1, new String[] { "planet" },
                new int[] { android.R.id.text1 });

        lv.setAdapter(simpleAdpt);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parentAdapter, View view,
                    int position, long id) {

                // We know the View is a TextView so we can cast it

                TextView clickedView = (TextView) view;

                Toast.makeText(
                        getActivity(),
                        "Item with id [" + id + "]  - Position [" + position
                                + "] - Planet [" + clickedView.getText() + "]",
                        Toast.LENGTH_SHORT).show();
            }

        });
        registerForContextMenu(lv);

        return view;

    }

    private void initList() {

        // We populate the planets

        planetsList.add(createPlanet("planet", "Mercury"));
        planetsList.add(createPlanet("planet", "Venus"));
        planetsList.add(createPlanet("planet", "Mars"));
        planetsList.add(createPlanet("planet", "Jupiter"));
        planetsList.add(createPlanet("planet", "Saturn"));
        planetsList.add(createPlanet("planet", "Uranus"));
        planetsList.add(createPlanet("planet", "Neptune"));

    }

    private HashMap<String, String> createPlanet(String key, String name) {

        HashMap<String, String> planet = new HashMap<String, String>();

        planet.put(key, name);

        return planet;

    }

    // We want to create a context Menu when the user long click on an item

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,

    ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);

        AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

        // We know that each row in the adapter is a Map

        HashMap map = (HashMap) simpleAdpt.getItem(aInfo.position);

        menu.setHeaderTitle("Options for " + map.get("planet"));
        menu.add(1, 1, 1, "Details");
        menu.add(1, 2, 2, "Delete");

    }
     @Override
      public void onItemAdded(Object data) {
          // to add item
          String string = String.valueOf(data);
          Toast.makeText(getContext(), "Data: " + string, Toast.LENGTH_SHORT).show();
          planetsList.add(createPlanet("planet", string));
      }
      @Override
    public void onStart() {
          super.onStart();

          DataModel.getInstance().setOnItemAddedHandler(this);
      }
}

这是片段库:

   import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;

      public class FragmentBase extends Fragment {

            private FragmentActivity mActivity; // I changed it to FragmentActivity because Activity was not working, and my `NavDrawer` is a FragmentActivity.

            public void setContext(FragmentActivity activity) {
                mActivity = mActivity;
            }

            public FragmentActivity getContext() {
                return mActivity;
            }
    }

这是数据模型:

  import android.util.Log;
    import com.xxx.xxx.QuickNoteFragment.OnItemAddedHandler;

public class DataModel {

    private static DataModel instance;
    private static OnItemAddedHandler mOnItemAddHandler;

    public static DataModel getInstance() {
        if (null == instance) {
            instance = new DataModel();
        }
        return instance;
    }

    public void setOnItemAddedHandler(OnItemAddedHandler handler) {
        mOnItemAddHandler = handler;
    }

    public void addItem(Object data) {
        if (null != mOnItemAddHandler)
            mOnItemAddHandler.onItemAdded(data);
        else {
            Log.i("is context null?", "yes!");
        }  
    }
}

这是 logcat 输出:

07-20 10:20:12.325: E/AndroidRuntime(14977): FATAL EXCEPTION: main
07-20 10:20:12.325: E/AndroidRuntime(14977): java.lang.NullPointerException
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.widget.Toast.<init>(Toast.java:92)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.widget.Toast.makeText(Toast.java:238)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at com.RiThBo.noter.HistoryFragment.onItemAdded(HistoryFragment.java:126)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at com.RiThBo.noter.DataModel.addItem(DataModel.java:24)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at com.RiThBo.noter.QuickNoteFragment.onClick(QuickNoteFragment.java:103)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.view.View.performClick(View.java:4211)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.view.View$PerformClick.run(View.java:17362)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.os.Handler.handleCallback(Handler.java:725)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.os.Looper.loop(Looper.java:137)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at android.app.ActivityThread.main(ActivityThread.java:5233)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at java.lang.reflect.Method.invoke(Method.java:511)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-20 10:20:12.325: E/AndroidRuntime(14977):    at dalvik.system.NativeStart.main(Native Method)

谢谢

4

1 回答 1

0

因为你想HistoryFragment在你去之后保持活跃QuickNoteFragment,所以你不应该replace片段,像下面的代码:

fragmentManager.beginTransaction().replace(R.id.content_frame, qnfragment).commit();

我已经修改了Navigation_Drawer以使所有片段保持活动状态。

private FragmentBase mCurrentFragment;

private void selectItem(int position) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    String fragmentTag = String.valueOf(position);

    FragmentBase fragment = (FragmentBase) fragmentManager.findFragmentByTag(fragmentTag);
    if (null == fragment) {
        fragment = createFragmentByPosition(position);
    }
    if (null == fragment)
        return;

    if (fragment.isAdded()) {
        fragmentTransaction.show(fragment);
    } else {
        fragmentTransaction.add(R.id.content_frame, fragment, fragmentTag);
    }

    if (mCurrentFragment != null) {
        fragmentTransaction.hide(mCurrentFragment);
    }
    mCurrentFragment = fragment;
    fragmentTransaction.commitAllowingStateLoss();

    mDrawerList.setItemChecked(position, true);
    setTitle(mNoterActivities[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

private FragmentBase createFragmentByPosition(int position) {
    FragmentBase fragment = null;
    if (position == 0) {
        fragment = new QuickNoteFragment();
        Bundle args = new Bundle();
        args.putInt(QuickNoteFragment.ARG_nOTERACTIVITY_NUMBER, position);
        fragment.setArguments(args);

    } else if (position == 1) {
        fragment = new PendViewPager();
        Bundle args = new Bundle();
        fragment.setArguments(args);
    }
    return fragment;
}

然后,在onItemAddedin 中HistoryFragment,仍然使用getActivity()

@Override
public void onItemAdded(Object data) {
    // to add item
    String string = String.valueOf(data);
    Toast.makeText(getActivity(), "Data: " + string, Toast.LENGTH_SHORT).show();
    planetsList.add(createPlanet("planet", string));
}

从以下代码中删除FragmentBase

private FragmentActivity mActivity; // I changed it to FragmentActivity
                                    // because Activity was not working, and
                                    // my `NavDrawer` is a FragmentActivity.

public void setContext(FragmentActivity activity) {
    mActivity = mActivity;
}

public FragmentActivity getContext() {
    return mActivity;
}

如果您没有在 中添加其他内容FragmentBase,它将是:

 class FragmentBase extends Fragment {

 }

是的,它是空的,但我建议保留它。

最后,如果HistoryFragment在后台,当应用程序返回后台时,它可能会被系统分离。所以从分离时注销自我DataModel

@Override
public void onDetach() {
    super.onDetach();
    DataModel.getInstance().setOnItemAddedHandler(null);
} 

更新1

@Override
public void onItemAdded(Object data) {
    // to add item
    String string = String.valueOf(data);
    planetsList.add(createPlanet("planet", string));
    simpleAdpt.notifyDataSetChanged();
}
于 2013-07-21T06:55:56.807 回答