如何将数据从适配器发送到 Android 中的 activity
How to send data from adapter into activity in Android
我想将数据从 适配器 发送到 activity 但没有 startActivity
.
我在 adapter 中写了下面的代码:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("sendDate", model.get(0).getLastSaleDate());
并在 activity 中写入以下代码:
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)
我该如何解决?
您使用 Intent 发送值而不是 Bundle.so 在 Activity
中使用 Intent 接收数据
Intent intent=this.getIntent();
if(intent !=null)// to avoid the NullPointerException
mainBoxOfficeDate.setText(intent.getStringExtra("sendDate"));
使用接口:-
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();
}
}
});
希望这对您有所帮助:-)
我想将数据从 适配器 发送到 activity 但没有 startActivity
.
我在 adapter 中写了下面的代码:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("sendDate", model.get(0).getLastSaleDate());
并在 activity 中写入以下代码:
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)
我该如何解决?
您使用 Intent 发送值而不是 Bundle.so 在 Activity
中使用 Intent 接收数据Intent intent=this.getIntent();
if(intent !=null)// to avoid the NullPointerException
mainBoxOfficeDate.setText(intent.getStringExtra("sendDate"));
使用接口:-
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();
}
}
});
希望这对您有所帮助:-)