Search code examples
androidnullpointerexceptionintentfilter

Intent filter and nullPointerException after launching activity


I am stuck and facing problem with intent-filter in android!
In my app when user taps on buy product button user will be redirected to internet browser (chrome etc) and after payment, user should come back to app, but occurred NullPinterException in onCreate Activity where I used TextView,ImageView.

I don't know why this problem happend, Please help!

ActivityPayment.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
        Utils.toast("onCreate");
        initUI();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        final ModelVoucher v = (ModelVoucher) extras.get("voucher");

            Picasso.with(G.currentActivity)
                    .load(G.URL + v.imageUrl)
                    .placeholder(R.mipmap.img_holder)
                    .error(R.mipmap.img_holder)
                    .into(imgVoucher);
            txtTitle.setText(v.title);
            txtUnitPrice.setText(v.payPrice);
            txtPayableAmount.setText((v.payPrice * count));

            List<String> list = new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("3");
            SpinnerAdapter adapter = new SpinnerAdapter(G.context, R.layout.spinner_item, list);
            spnCount.setAdapter(adapter);

            spnCount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    String selected = spnCount.getSelectedItem().toString();
                    count = Integer.valueOf(selected.trim());
                    txtPayableAmount.setText(v.payPrice * count );
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });

            lyFinalizeBuy.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    new RequestPayment(new IRequest() {
                        @Override
                        public void onCompeletd(final ModelResult result) {
                            G.handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (result.success) {
                                        Intent paymentPage = new Intent(Intent.ACTION_VIEW);
                                        paymentPage.setData(Uri.parse(G.URL + "/payment/pay/" + result.paymentHash));
                                        startActivity(paymentPage);
                                    } else {
                                        Utils.toast(result.message, true);
                                    }
                                }
                            });
                        }

                        @Override
                        public void onFailed(int status) {

                        }
                    }).beforePay(v.id + "", count + "");


                }
            });

        }

        new RequestProfile(new IRequest() {
            @Override
            public void onCompeletd(final ModelResult result) {
                G.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (result.success) {
                            txtBalance.setText(result.user.balance);
                        } else {
                            Utils.toast(result.message);
                        }
                    }
                });
            }

            @Override
            public void onFailed(int status) {

            }
        }).getBaseInfo(); 

}

Stack Trace

> 12-04 12:56:26.507  12350-12350/ir.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.myapplication/ir.myapplication.ActivityPayment}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
            at android.app.ActivityThread.access$600(ActivityThread.java:165)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at ir.myapplication.ActivityPayment.onCreate(ActivityPayment.java:56)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
            at android.app.ActivityThread.access$600(ActivityThread.java:165)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

56 line that happend exception:

 Picasso.with(G.currentActivity)
                .load(G.URL + v.imageUrl)
                .placeholder(R.mipmap.img_holder)
                .error(R.mipmap.img_holder)
                .into(imgVoucher);

Solution

  • Can you please use below code ?

    There is issue with Null Pointer.

    How to send object:

    ClassName details = new ClassName();
    Intent i = new Intent(context, EditActivity.class);
    i.putExtra("Editing", details);
    startActivity(i);
    

    How to receive object:

    ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");
    

    Your model class must be implemented as Serializable.

    Class ClassName implements Serializable {
    } 
    

    I hope your issue would resolve.