我希望将适配器的数据发送到活动,但不包含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)
我该如何解决?
答案 0 :(得分:1)
您使用Intent而不是Bundle发送了值。因此,使用Intent在Activity
中接收数据Intent intent=this.getIntent();
if(intent !=null)// to avoid the NullPointerException
mainBoxOfficeDate.setText(intent.getStringExtra("sendDate"));
答案 1 :(得分:1)
使用界面: -
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();
}
}
});
希望这会有所帮助: - )