NullPointerException getArguments()但是有数据

时间:2015-02-06 07:48:22

标签: android android-fragments nullpointerexception android-viewpager

我不明白为什么无效。我使用Log check有数据

请给我解决方案!

我的代码在这里

我将数据从ViewPagerActivity传递到PlayRadio

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main_activity);
        Intent intent = getIntent();
        Bundle args = new Bundle();
        args = intent.getExtras();
        Fragment playRadio = new PlayBlog();
        playRadio.setArguments(args);
        FragmentManager fragment = getSupportFragmentManager();
        FragmentTransaction transaction = fragment.beginTransaction();
        transaction.replace(R.id.pager, playRadio);
        transaction.commit();
        setUpView();
    }

在PlayRadio中,我获得了价值

public class PlayBlog extends Fragment {
    Context mContext;
    String Desciption;

    public static Fragment newInstance(Context context) {
        PlayBlog playBlog = new PlayBlog();
        return playBlog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.playradio_layout, container,
                false);
        mContext = container.getContext();
        final Animation anim = AnimationUtils.loadAnimation(mContext,
                R.anim.rotate);
        ImageView splash = (ImageView) rootView.findViewById(R.id.base);
        TextView tvDesciption = (TextView) rootView
                .findViewById(R.id.tv_Marquee);
        Bundle args = this.getArguments();
        Desciption = args.getString(Constant.STRING_KEY);
        Log.i("", Constant.TAG + "PlayBlog " + Desciption);
        tvDesciption.setText(Desciption);
        splash.startAnimation(anim);
        return rootView;
    }

}

Error at line : Desciption = args.getString(Constant.STRING_KEY);

logcat错误

02-06 07:41:49.362: E/AndroidRuntime(2534): FATAL EXCEPTION: main
02-06 07:41:49.362: E/AndroidRuntime(2534): java.lang.NullPointerException
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.tadev.blogradio.main.PlayBlog.onCreateView(PlayBlog.java:37)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.View.measure(View.java:15518)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.View.measure(View.java:15518)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.View.measure(View.java:15518)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.View.measure(View.java:15518)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.View.measure(View.java:15518)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.os.Handler.handleCallback(Handler.java:725)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.os.Looper.loop(Looper.java:137)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at java.lang.reflect.Method.invoke(Method.java:511)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-06 07:41:49.362: E/AndroidRuntime(2534):     at dalvik.system.NativeStart.main(Native Method)

但是当我检查Desciption时Logcat给出了数据。我不明白:(

02-06 07:41:49.354: I/(2534): abc PlayBlog Blog Radio - Khi trưởng thành, bạn hay mang trong mình cảm giác lưng chừng cô đơn. Vì, không hẳn là không có người để quan tâm, chăm sóc, nhưng tại một thời điểm nào đó, một phút giây nào đó, bạn vẫn cảm thấy mình chông chênh và lạc lõng giữa chốn đông người...

更新代码

类NewRadioFragment

btnShowMore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getLink_play() != null) {
                    Toast.makeText(mContext, item.getLink_play(),
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext,
                            ViewPagerActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.STRING_KEY, Desciption);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(mContext, "NULL", Toast.LENGTH_SHORT).show();
                }
            }
        });

我将数据从NewRadioFragment传递到ViewPagerActivity(Bunble)并发送PlayRadio继续

3 个答案:

答案 0 :(得分:2)

我认为你遗忘了putString

args
args.putString(Constant.STRING_KEY,"testing");
playRadio.setArguments(args);

答案 1 :(得分:0)

在第一点开始

Bundle args = intent.getExtras();
// Here args is null at the first run

args 为null,因为intent中没有数据。如果你需要发送一些数据。创建一个intent或bundle然后放入你的数据

Bundle args = new Bundle();
args.putString(Constant.STRING_KEY,"testing");
playRadio.setArguments(args);

答案 2 :(得分:0)

我认为问题在于在Fragment类中接收数据。

使用代码:

String description = getArguments().getString(YOUR_KEY); // Simply use getArguments istead of this.getArguments()

它会起作用。