我正在使用主详细信息流模板通过SAP Mobile Platform从SAP显示三个表。从第一个只是工作订单标题列表的活动中,我选择一个标题(工作订单)并显示主活动(ItemListActivity)中的项目列表。我可以做,但现在当我点击项目时,我必须转发一个Long变量(surrogateKey),以便我可以加载该项目的详细信息以及另外两个详细活动列表。我做了布局但是当我运行它时会在ItemDetailFragment的onCreateView中抛出nullPointerException错误。 这是来自ItemListFragment:
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
Item item = items.get(position);
long surrogateKey = item.getSurrogateKey();
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(surrogateKey);
}
这来自ItemListActivity:
@Override
public void onItemSelected(Long surrogateKey) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putLong(ItemDetailFragment.ARG_ITEM_ID, surrogateKey);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment).commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(ItemListActivity.this, ItemDetailActivity.class);
detailIntent.putExtra("surrogateKey", surrogateKey);
startActivity(detailIntent);
}
}
这是来自ItemDetailActivity:
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.getLong(ItemDetailFragment.ARG_ITEM_ID, 0);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container, fragment).commit();
在onDreate of ItemDetailFragment:
surrogateKeyItem = getArguments().getLong(ARG_ITEM_ID);
具有正确的值(来自调试),但在onCreateView中,它在setText上中断:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
Item item = Item.find(surrogateKeyItem);
final EditText psmc = (EditText)this.getActivity().findViewById(R.id.editPsmc);
psmc.setText(item.getPOCETNOSMC().toString());
我做错了什么?
这是来自LogCat:
02-03 23:33:41.889:D / AndroidRuntime(22361):关闭VM 02-03 23:33:41.889:W / dalvikvm(22361):threadid = 1:线程退出未捕获的异常(group = 0x415622a0)02-03 23:33:41.944:E / AndroidRuntime(22361):FATAL EXCEPTION:main 02-03 23:33:41.944:E / AndroidRuntime(22361):java.lang.NullPointerException 02-03 23:33: 41.944:E / AndroidRuntime(22361):at andrzg.zaga.app.md.ItemDetailFragment.onCreateView(ItemDetailFragment.java:97)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.support.v4 .app.Fragment.performCreateView(Fragment.java:1500)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)02- 03 23:33:41.944:E / AndroidRuntime(22361):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.suppor t.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java: 440)02-03 23:33:41.944:E / AndroidRuntime(22361):在android.os.Handler.handleCallback(Handler.java:615)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.os.Handler.dispatchMessage(Handler.java:92)02-03 23:33:41.944:E / AndroidRuntime(22361):at android.os.Looper.loop(Looper.java:137)02-03 23: 33:41.944:E / AndroidRuntime(22361):在android.app.ActivityThread.main(ActivityThread.java:4895)02-03 23:33:41.944:E / AndroidRuntime(22361):at java.lang.reflect.Method .invokeNative(Native Method)02-03 23:33:41.944:E / AndroidRuntime(22361):at java.lang.reflect.Method.invoke(Method.java:511)02-03 23:33:41.944:E / AndroidRuntime(22361):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994)02-03 23:33:41.944:E / AndroidRuntime(22361):at com.android.internal.os .ZygoteInit .main(ZygoteInit.java:761)02-03 23:33:41.944:E / AndroidRuntime(22361):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
是fragment_item_detail布局文件中的editPsmc EditText吗?
如果是,你应该像这样抓住EditText:
final EditText psmc = (EditText)rootView.findViewById(R.id.editPsmc);
我认为你的空指针是因为EditText psmc为null而你不能在null对象上调用setText()。
修改强>
在您的活动中,您希望以这种方式检索密钥:
long key = getIntent().getLongExtra("surrogateKey", 0);
祝你好运!