【问题标题】:sending Intent from a fragment从片段发送意图
【发布时间】:2018-06-07 14:19:05
【问题描述】:

尝试将 Intent 从 Fragment 发送到 Activity,但被困在那里。任何线索都会很棒:)

在我的片段中,我有这个:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.customer_detail, container, false);


        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.customer_hidden_id)).setText(String.valueOf(mItem.getCustomerId()));
            ((TextView) rootView.findViewById(R.id.customer_lastname)).setText(mItem.getLastName());
            ((TextView) rootView.findViewById(R.id.customer_firstname)).setText(mItem.getFirstName());
            ((TextView) rootView.findViewById(R.id.customer_address)).setText(mItem.getAddress());
            ((TextView) rootView.findViewById(R.id.customer_postcode)).setText(mItem.getPostCode());
            ((TextView) rootView.findViewById(R.id.customer_city)).setText(mItem.getCity());
            ((TextView) rootView.findViewById(R.id.customer_phone)).setText(mItem.getPhone());
            ((TextView) rootView.findViewById(R.id.customer_mail)).setText(mItem.getEmail());
            ((TextView) rootView.findViewById(R.id.customer_license)).setText(mItem.getLicenseNumber());

            Intent intent;
            intent = new Intent(getActivity(), CustomerDetailActivity.class );
            intent.putExtra("customer", mItem);
            getActivity().startActivity(intent);

        }

        return rootView;
    }

在活动中,我尝试获取 Intent:

public class CustomerDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_detail);
        Toolbar toolbar = findViewById(R.id.detail_toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        Customer currentCustomer = intent.getParcelableExtra("customer");
        Log.i("CUST", currentCustomer.toString());
}

但是我得到一个 nullPointerException 所以 Intent 没有通过。如果你们知道方法...

LogCat 错误:

06-07 14:36:53.299 3224-3224/com.eni.goj.lokacar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.eni.goj.lokacar, PID: 3224
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eni.goj.lokacar/com.eni.goj.lokacar.Activities.CustomerDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.eni.goj.lokacar.BO.Customer.toString()' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                       at android.os.Looper.loop(Looper.java:164)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.eni.goj.lokacar.BO.Customer.toString()' on a null object reference
                                                                       at com.eni.goj.lokacar.Activities.CustomerDetailActivity.onCreate(CustomerDetailActivity.java:34)
                                                                       at android.app.Activity.performCreate(Activity.java:7009)
                                                                       at android.app.Activity.performCreate(Activity.java:7000)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                       at android.os.Looper.loop(Looper.java:164) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

【问题讨论】:

  • 使用包你不能在意图中传递可解析的对象
  • 你甚至没有从你的代码开始你的活动......
  • 你能显示你开始活动的代码部分吗
  • 哪一行导致 NullPointerException?
  • 在一个片段中,你必须做getActivity().startActivity(intent);

标签: java android android-fragments android-intent


【解决方案1】:
Intent intent;
intent = new Intent(getActivity(), CustomerDetailActivity.class);
intent.putExtra("customer", mItem);
getActivity().startActivity(intent);

【讨论】:

    【解决方案2】:

    您的意图正在传递,但处理它的代码正在引发异常。

    Customer currentCustomer = intent.getParcelableExtra("customer");
    Log.i("CUST", currentCustomer.toString());
    

    currentCustomer 为 null,因此尝试访问其 toString 方法会引发异常。

    What is a NullPointerException, and how do I fix it?

    【讨论】:

    • onCreateView()被调用之前不是附加了activity吗?
    • 刚刚为您添加了 LogCat :)
    猜你喜欢
    • 2017-02-03
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多