无法从活动中获取片段中的bundle参数

时间:2015-07-04 17:07:09

标签: android android-fragments android-bundle

我是Android开发人员(目前是PHP程序员)。我正在尝试通过谷歌开发人员和其他网站的各种教程来学习android。

我的问题是我无法在我的片段中获得我的行为中设置的包参数。

以下是我的HomeActivity的方法:

public void SelectItem(int possition) {
    Fragment fragment = new FragmentImageList();
    Bundle bundle = new Bundle();
    if (possition <= 3) {

        bundle.putString("test_string", "Hello test");
        fragment.setArguments(bundle);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.image_list_frame, fragment)
                .commit();
    }

}

这是我的FragmentImageList片段:

public FragmentImageList() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    String strtext=getArguments().getString("test_string");
    Log.d("test string",strtext);
    Bundle bundle = this.getArguments();
    Log.d(TAG,  bundle.getString("test_string"));
    View view = inflater.inflate(R.layout.fragment_image_list, container,
            false);
    GridView gridView = (GridView) view.findViewById(R.id.gridView);
    gridView.setAdapter(new GridViewAdapter());
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String url = (String) view.getTag();
            Log.d(TAG,  url);
            Intent intent = new Intent(view.getContext(), DetailActivity.class);
            intent.putExtra("EXTRA_IMAGE", url);
            startActivity(intent);


        }
    });
    return view;
}

我的错误日志

    07-04 22:16:49.908    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
    07-04 22:16:49.908    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
    07-04 22:16:50.033  14550-14550/? E/PhotosPlugin﹕ Loading PhotosPlugin
    07-04 22:16:50.480  14620-14620/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.sujit.wallpapersnepal, PID: 14620
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sujit.wallpapersnepal/com.sujit.wallpapersnepal.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
    at com.sujit.wallpapersnepal.FragmentImageList.onCreateView(FragmentImageList.java:37)
    at android.app.Fragment.performCreateView(Fragment.java:2053)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.app.BackStackRecord.run(BackStackRecord.java:834)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
    at android.app.Activity.performStart(Activity.java:6005)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    07-04 22:16:52.006  14729-14729/? E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
    07-04 22:16:52.782  14820-14820/? E/ObjectHelper﹕ Can't find method:setCompatibilityInfo
    07-04 22:16:54.497  15063-15063/? E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
    07-04 22:16:54.613    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app

String strtext = getArguments()。getString(“test_string”);不行。我也试过了 Bundle bundle = this.getArguments();  Log.d(TAG,bundle.getString(“test_string”));但它也没有用。但是,如果我评论这些捆绑线,那么应用程序运行正常,没有错误。这个“test_string”只是一个测试参数,我试图让它工作。所以,我想知道我做了什么错误,这给了我这个错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sujit.wallpapersnepal/com.sujit.wallpapersnepal.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

已编辑:我正在尝试在我的Nexus 5 Android版本5.1.1中运行此应用程序

2 个答案:

答案 0 :(得分:2)

考虑使用fragments guide(靠近底部)的工厂方法。可以找到这样做的原因here.

FragmentImageList片段中添加以下静态方法。

public static FragmentImageList newInstance(String string) {
    FragmentImageList f = new FragmentImageList();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putString("test_string", string);
    f.setArguments(args);

    return f;
}

并修改您的onCreate()方法

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString("test_string");
    }
}

在您的SelectItem方法中,

Fragment fragment = FragmentImageList.newInstance("Hello test");

答案 1 :(得分:0)

您可以尝试更改此行吗

捆绑包= this.getArguments();

String value = getArguments()。getString(“key”); Log.i( “机器人”,值);

此外,我不确定,您是否应该获取捆绑包然后给视图充气, 反过来说,首先给视图充气,然后拿出包

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(MainActivity.TAG,"Fragment onCreateView()");
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String value = getArguments().getString("key");
        Log.i("ANDROID",value);

        return rootView;

    }