1

I want send data from adapter into activity but without startActivity.
I write below codes in adapter :

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("sendDate", model.get(0).getLastSaleDate());

And write below codes in activity :

bundle = getIntent().getExtras();

mainBoxOfficeDate.setText(bundle.getString("sendDate"));

Show me this error :

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 Activities.MainActivity.onCreate(MainActivity.java:53)
                                                                             at android.app.Activity.performCreate(Activity.java:6754)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6247) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

How can I fix it?

2 Answers2

2

Use Interface:-

public interface IMethodCaller{
void yourDesiredMethod(String text);

}

Call interface when clicking the button or any action item:-

Button btn=(Button)convertView.findViewById(yourButtonId);

btn.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
    if(mContext instanceof IMethodCaller){
        ((IMethodCaller)mContext).yourDesiredMethod();
    }
}

});

Hope this helps :-)

Jeeva
  • 1,166
  • 4
  • 18
  • 39
1

you sent value using Intent instead of Bundle.so use Intent for receive data in Activity

Intent intent=this.getIntent();
if(intent !=null)// to avoid the NullPointerException
 mainBoxOfficeDate.setText(intent.getStringExtra("sendDate"));
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Thanks my friend, in your code not show me error, but in textView not set any text! how can I fix it? –  May 25 '17 at 17:51
  • In adapter I show this model.get(0).getLastSaleDate()); into toast and show data ! but in activity show null –  May 25 '17 at 17:55
  • please help me my friend, I really need this help, please –  May 25 '17 at 17:56
  • send data like this....Intent intent = new Intent(context, MainActivity.class); intent.putExtra("sendDate", model.get(0).getLastSaleDate()); context.startActivity(intent); – sasikumar May 25 '17 at 18:00
  • This is not the correct solution @sasikumar. this will recreate the activity again. – Jeeva May 25 '17 at 18:08
  • then use sharedpreference for passing data without startActivityhttps://stackoverflow.com/questions/12030276/android-putextra-without-start-activity – sasikumar May 25 '17 at 18:14