我想将数据从适配器发送到活动中,但没有startActivity
。
我在适配器中写了以下代码:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("sendDate", model.get(0).getLastSaleDate());
并在活动中编写以下代码:
bundle = getIntent().getExtras();
mainBoxOfficeDate.setText(bundle.getString("sendDate"));
给我显示此错误:
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)
我该如何解决它?
使用接口:-
public interface IMethodCaller{
void yourDesiredMethod(String text);
}
单击按钮或任何操作项时调用界面:-
Button btn=(Button)convertView.findViewById(yourButtonId);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if(mContext instanceof IMethodCaller){
((IMethodCaller)mContext).yourDesiredMethod();
}
}
}(;
希望这有帮助:-(
您使用 Intent 而不是使用 Intent 在活动中接收数据 Bundle.so 发送值
Intent intent=this.getIntent();
if(intent !=null)// to avoid the NullPointerException
mainBoxOfficeDate.setText(intent.getStringExtra("sendDate"));