отправка Намерения из фрагмента - PullRequest
0 голосов
/ 07 июня 2018

Пытаюсь отправить Намерение из фрагмента в 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;
    }

и в упражнении я пытаюсь получить намерение:

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, поэтому намерение не передается.Если вы, ребята, знаете способ ...

Ошибка 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)

Ответы [ 2 ]

0 голосов
/ 07 июня 2018

Ваше намерение передается, но код, обрабатывающий его, вызывает исключение.

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

currentCustomer имеет значение null, поэтому при попытке доступа к его методу toString возникло исключение.

Что такое исключение NullPointerException и как его исправить?

0 голосов
/ 07 июня 2018
Intent intent;
intent = new Intent(getActivity(), CustomerDetailActivity.class);
intent.putExtra("customer", mItem);
getActivity().startActivity(intent);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...