我基本上有一个活动MainActivity.java包含两个片段Fragment1.java和Fragment2.java。
由包含各种选择的listfragment组成,
包含一个ImageView,它根据从Fragment1中选择的选项显示图像。
我已经设法在Fragment1中创建一个onListItemClick侦听器,它将已经单击的列表位置发送回MainActivity,然后将其发送到Fragment2,Fragment2然后根据此位置值显示图像。
然而,我似乎遇到了一堵砖墙,在开始我的活动时,我在Fragment2.java中收到以下错误:
java.lang.NullPointerException
我认为这是因为尚未在第一个Fragment1.java上单击列表项,因此Fragment2.java没有将ImageView图像设置为的位置值。这有一个简单的方法吗?例如,最初设置默认图像,然后根据所选的列表项更改它?
由于
代码:
好的,所以Fragment1包含以下侦听器和intent,传递给CreateActivity:
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity().getBaseContext(),
CreateActivity.class);
intent.putExtra("position", position);
page = 1;
((CreateActivity) getActivity()).mViewPager.setCurrentItem(page);
}
然后由CreateActivity接收并发送到Fragment2:
public void SendData() {
Intent intent = getIntent();
int position = intent.getIntExtra("position", 1);
Bundle bundle = new Bundle();
bundle.putInt("myData", position);
Fragment2 fragobj = new Fragment2();
fragobj.setArguments(bundle);
}
片段2然后重现这个意图并用它来设置ImageView,这里我输出到日志只是为了检查意图是否正常工作:
int Position = getArguments().getInt("myData");
if (Position == 1) {
Log.w("myApp", "Intent Recieved");
}
继承我的logcat:
08-29 12:54:18.844: E/AndroidRuntime(22878): FATAL EXCEPTION: main
08-29 12:54:18.844: E/AndroidRuntime(22878): java.lang.NullPointerException
08-29 12:54:18.844: E/AndroidRuntime(22878): at com.meme.hdmeme.Fragment2.onCreateView(Fragment2.java:50)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.View.measure(View.java:15518)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.View.measure(View.java:15518)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.View.measure(View.java:15518)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-29 12:54:18.844: E/AndroidRuntime(22878): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2313)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.View.measure(View.java:15518)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.os.Handler.handleCallback(Handler.java:725)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.os.Looper.loop(Looper.java:137)
08-29 12:54:18.844: E/AndroidRuntime(22878): at android.app.ActivityThread.main(ActivityThread.java:5195)
08-29 12:54:18.844: E/AndroidRuntime(22878): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 12:54:18.844: E/AndroidRuntime(22878): at java.lang.reflect.Method.invoke(Method.java:511)
08-29 12:54:18.844: E/AndroidRuntime(22878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-29 12:54:18.844: E/AndroidRuntime(22878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-29 12:54:18.844: E/AndroidRuntime(22878): at dalvik.system.NativeStart.main(Native Method)