这可能是一个冗长的帖子,所以我提前道歉.
我正在使用主/详细信息流来显示项目列表,单击它时会打开详细信息视图.这些项目是从Web服务加载的.
它可以在平板电脑上使用碎片很好用,但在手机上却不断崩溃.它可以正确显示项目详细信息(CheatVIEwPageIndicator.java)但是当我使用 *** 作栏左上角的“向上”按钮返回到父活动(CheatListActivity.java)时,应用程序会一直崩溃并出现空指针异常.我想我正在从错误的位置加载来自Web服务的数据,这就是它崩溃的原因.我打算在这里编写我的代码,希望有人可以给我一个建议,我将如何以正确的方式做到这一点.
(我已经修改了一些类来缩短帖子.)
“主”活动:
public class CheatListActivity extends FragmentActivity implements CheatListFragment.Callbacks,ReportCheatDialogListener,RateCheatDialogListener { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_cheat_List); settings = getSharedPreferences(Konstanten.PREFERENCES_file,0); editor = settings.edit(); cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); cheatProgressDialog = ProgressDialog.show(this,getString(R.string.please_wait) + "...",getString(R.string.retrIEving_data) + "...",true); handleIntent(getIntent()); if (findVIEwByID(R.ID.cheat_detail_container) != null) { // The detail container vIEw will be present only in the // large-screen layouts (res/values-large and // res/values-sw600dp). If this vIEw is present,then the // activity should be in two-pane mode. mTwoPane = true; // In two-pane mode,List items should be given the // 'activated' state when touched. ((CheatListFragment) getSupportFragmentManager().findFragmentByID(R.ID.cheat_List)).setActivateOnItemClick(true); } cheatProgressDialog.dismiss(); // Todo: If exposing deep links into your app,handle intents here. } private voID handleIntent(final Intent intent) { new Thread(new Runnable() { @OverrIDe public voID run() { gameObj = new Gson().fromJson(intent.getStringExtra("gameObj"),Game.class); runOnUiThread(new Runnable() { @OverrIDe public voID run() { getActionbar().setdisplayHomeAsUpEnabled(true); getActionbar().setTitle(gameObj.getGamename()); getActionbar().setSubTitle(gameObj.getSystemname()); } }); try { if (cm.getActiveNetworkInfo() != null) { if (member == null) { cheats = Webservice.getCheatList(gameObj,0); } else { cheats = Webservice.getCheatList(gameObj,member.getMID()); } cheatsArrayList = new ArrayList
列表视图的片段.
public class CheatListFragment extends ListFragment { /** * The serialization (saved instance state) Bundle key representing the * activated item position. Only used on tablets. */ private static final String STATE_ACTIVATED_position = "activated_position"; /** * The fragment's current callback object,which is notifIEd of List item * clicks. */ private Callbacks mCallbacks = sDummyCallbacks; /** * The current activated item position. Only used on tablets. */ private int mActivatedposition = ListVIEw.INVALID_position; /** * A callback interface that all activitIEs containing this fragment must * implement. This mechanism allows activitIEs to be notifIEd of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public voID onItemSelected(int position); } /** * A dummy implementation of the {@link Callbacks} interface that does * nothing. Used only when this fragment is not attached to an activity. */ private static Callbacks sDummyCallbacks = new Callbacks() { @OverrIDe public voID onItemSelected(int ID) { } }; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orIEntation changes). */ public CheatListFragment() { } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cheatListActivity = (CheatListActivity) getActivity(); FontRoboto = Tools.getFontRobotoRegular(getActivity().getAssets()); settings = cheatListActivity.getSharedPreferences(Konstanten.PREFERENCES_file,0); gameObj = new Gson().fromJson(settings.getString(Konstanten.PREFERENCES_TEMP_GAME_OBJECT_VIEW,null),Game.class); if( gameObj == null) { new GetCheatsTask().execute(new Game()); } else { new GetCheatsTask().execute(gameObj); } } private class GetCheatsTask extends AsyncTask
双窗格模式下的详细视图(在平板电脑上):
public class CheatDetailtabletFragment extends Fragment implements OnClickListener { /** * The fragment argument representing the item ID that this fragment * represents. */ public static final String ARG_ITEM_ID = "item_ID"; /** * The dummy content this fragment is presenting. */ private CheatContent.CheatItem mItem; private VIEw rootVIEw; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orIEntation changes). */ public CheatDetailtabletFragment() { } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ca = (CheatListActivity) getActivity(); cheatTitleTypeface = Tools.getFontRobotoThin(getActivity().getAssets()); cheatTextTypeface = Tools.getFontRobotoRegular(getActivity().getAssets()); settings = getActivity().getSharedPreferences(Konstanten.PREFERENCES_file,0); editor = settings.edit(); if (getArguments().containsKey(ARG_ITEM_ID)) { // Load the dummy content specifIEd by the fragment // arguments. In a real-world scenario,use a Loader // to load content from a content provIDer. mItem = CheatContent.ITEM_MAP.get(getArguments().getInt(ARG_ITEM_ID)); cheatObj = new Gson().fromJson(getArguments().getString("cheatObj"),Cheat.class); cheatForumFragment = new Gson().fromJson(getArguments().getString("cheatForumFragment"),CheatForumFragment.class); cheatDetailMetaFragment = new Gson().fromJson(getArguments().getString("cheatDetailMetaFragment"),CheatDetailMetaFragment.class); } } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { rootVIEw = inflater.inflate(R.layout.fragment_cheat_detail,container,false); // ... // Show the dummy content as text in a TextVIEw. if (mItem != null) { ((TextVIEw) rootVIEw.findVIEwByID(R.ID.text_cheat_before_table)).setText(mItem.getCheatTitle()); ((TextVIEw) rootVIEw.findVIEwByID(R.ID.text_cheat_Title)).setText(mItem.getCheatID()); } // ... populateVIEw(); return rootVIEw; } /** * Create Layout */ private voID populateVIEw() { // fills the vIEw. no problems here. } /** * Populate table Layout */ private voID filltableContent() { // filling table content here. no problems here. } private voID fillSimpleContent() { // filling with other content. works fine,too. }}
单窗格模式下的详细信息视图(在电话上):
public class CheatVIEwPageIndicator extends FragmentActivity implements ReportCheatDialogListener,RateCheatDialogListener { // define variables... public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = LayoutInflater.from(this); intent = getIntent(); vIEwLayout = inflater.inflate(intent.getIntExtra("layoutResourceID",R.layout.activity_cheatvIEw_pager),null); setContentVIEw(vIEwLayout); settings = getSharedPreferences(Konstanten.PREFERENCES_file,0); editor = settings.edit(); getActionbar().setHomebuttonEnabled(true); getActionbar().setdisplayHomeAsUpEnabled(true); try { gameObj = new Gson().fromJson(settings.getString(Konstanten.PREFERENCES_TEMP_GAME_OBJECT_VIEW,Game.class); if (gameObj == null) { gameObj = new Gson().fromJson(intent.getStringExtra("gameObj"),Game.class); } editor.putString(Konstanten.PREFERENCES_TEMP_GAME_OBJECT_VIEW,new Gson().toJson(gameObj)); editor.commit(); pageSelected = intent.getIntExtra("selectedPage",0); activePage = pageSelected; pageIndicatorcolor = intent.getIntExtra("pageIndicatorcolor",Konstanten.CYAN_DARK); cheatObj = gameObj.getCheats(); visibleCheat = cheatObj[pageSelected]; getActionbar().setTitle(gameObj.getGamename()); getActionbar().setSubTitle(gameObj.getSystemname()); initialisePaging(); } catch (Exception e) { Log.e(CheatVIEwPageIndicator.class.getname(),e.getMessage() + ""); } } private voID initialisePaging() { // ... } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.handset_cheatvIEw_menu,menu); // Search getMenuInflater().inflate(R.menu.search_menu,menu); // Associate searchable configuration with the SearchVIEw SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchVIEw searchVIEw = (SearchVIEw) menu.findItem(R.ID.search).getActionVIEw(); searchVIEw.setSearchableInfo(searchManager.getSearchableInfo(getComponentname())); return super.onCreateOptionsMenu(menu); } @OverrIDe protected voID onResume() { super.onResume(); invalIDateOptionsMenu(); } public ConnectivityManager getConnectivityManager() { if (cm == null) { cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); } return cm; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { switch (item.getItemID()) { case androID.R.ID.home: // This ID represents the Home or Up button. In the case of this // activity,the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details,see the Navigation pattern on AndroID Design: // // http://developer.androID.com/design/patterns/navigation.HTML#up-vs-back // onBackpressed(); // return true; Intent upIntent = new Intent(this,CheatListActivity.class); NavUtils.getParentActivityIntent(this); if (NavUtils.shouldUpRecreateTask(this,upIntent)) { // This activity is NOT part of this app's task,so create a new // task when navigating up,with a synthesized back stack. TaskStackBuilder.create(this) // Add all of this activity's parents to the back stack .addNextIntentWithParentStack(upIntent) // Navigate up to the closest parent .startActivitIEs(); } else { // This activity is part of this app's task,so simply // navigate up to the logical parent activity. // NavUtils.navigateUpTo(this,upIntent); upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_top); startActivity(upIntent); finish(); } return true; } return super.onoptionsItemSelected(item); } }
因此,在单窗格模式(在手机上),当我从CheatVIEwPageIndicator.java单击 *** 作栏中的“向上”按钮以获取CheatListActivity.java时,我得到一个指向此行的nullpointer异常:
gameObj = new Gson().fromJson(intent.getStringExtra("gameObj"),Game.class);
回来时似乎“intent”为null.我想知道为什么会这样?我需要做什么才能将数据保存在意图中? (或任何其他解决方案如何摆脱nullpointer对我也完全没问题).我有点绝望,我一直在寻找解决方案太久了.
非常感谢您的帮助.
最佳答案我几乎可以肯定问题来自CheatVIEwPageIndicator中的onoptionsItemSelected().您不需要在那里开始新的活动,在主 – 细节模式中,一次总是有一个相同的活动.只有片段在变化.要提供正确使用“向上”按钮的功能,您应该在添加片段时调用fragmentTransaction.addToBackStack(null). AndroID将自行处理所有堆栈逻辑.在androID.R.ID.home中根本不做任何事情,你不需要它.
总结以上是内存溢出为你收集整理的android – 在 *** 作栏中单击“向上”时主/详细流程中的空意图(nullpointer异常)全部内容,希望文章能够帮你解决android – 在 *** 作栏中单击“向上”时主/详细流程中的空意图(nullpointer异常)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)