我有两个片段,QuickNoteFragment和HistoryFragment,它们是NavigationDrawer的一部分(这是它们所绑定的FragmentActivity)。在QuickNoteFragment中,我有一个按钮。按下此按钮时,我希望在HistoryFragment中将另一项添加到ListView。
我问了这个问题:change the items in a ListView from another fragment并从srain得到了一个很好的解决方案。但是当我这样做时,我得到了一个nullpointerexception。我们有一个很长(太长)的评论对话试图找到解决方案,但无济于事。所以问题是:我如何摆脱这个例外?这是我重现它的方式
打开应用 - >它会自动启动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(请记住它是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);
}
}
这是FragmentBase:
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;
}
}
这是DataModel:
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)
谢谢
答案 0 :(得分: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;
}
然后,在onItemAdded
的{{1}}中,仍然使用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
<强> UPDATE1 强>
@Override
public void onDetach() {
super.onDetach();
DataModel.getInstance().setOnItemAddedHandler(null);
}