0

我基本上有一个活动 MainActivity.java,其中包含两个片段 Fragment1.java 和 Fragment2.java。

片段1.java

由具有各种选择的列表片段组成,

片段2.java

由一个 ImageView 组成,它根据从 Fragment1 中选择的选项显示图像。

我设法在 Fragment1 中创建了一个 onListItemClick 侦听器,它将已单击的列表位置发送回 MainActivity,然后将其发送到 Fragment2,Fragment2 然后根据此位置值显示图像。

但是,我似乎遇到了一堵砖墙,在开始我的活动时,我在 Fragment2.java 中收到以下错误:

java.lang.NullPointerException

我认为这是因为尚未在第一个 Fragment1.java 上单击列表项,因此 Fragment2.java 没有将 ImageView 图像设置为的位置值。有没有一种简单的方法来解决这个问题?例如,最初设置默认图像,然后根据所选列表项更改它?

谢谢

代码:

好的,所以 Fragment1 包含以下侦听器和意图,传递给 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);
    }

Fragment 2 然后接收此意图并使用它来设置 ImageView,这里我输出到日志只是为了检查意图是否正常工作:

    int Position = getArguments().getInt("myData");
    if (Position == 1) {
        Log.w("myApp", "Intent Recieved");
    }

这是我的日志:

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)
4

0 回答 0