我正在尝试从Android的自定义适配器Activity
启动ListView
。我正在声明上下文,但Logcat显示找不到Context。我在这里上传我的代码
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
//context = this;
if (v == null) {
holder = new ViewHolder();
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.mipmap.ic_launcher);
new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.tvName.setText(actorList.get(position).getName());
holder.tvDescription.setText(actorList.get(position).getDescription());
holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
holder.tvCountry.setText(actorList.get(position).getCountry());
holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());
holder.tvName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), actorList.get(position).getName(), Toast.LENGTH_SHORT).show();
Context context = getContext();
Intent i = new Intent(context, Details.class);
context.startActivity(i);
}
});
return v;
}
在这里,我想针对Activity
点击事件开始新的Button's
。
这是我的LogCat
06-25 14:25:15.237 3179-3179/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.scroll.owner.jsonparsing, PID: 3179
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1421)
at android.app.ContextImpl.startActivity(ContextImpl.java:1408)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:324)
at com.scroll.owner.jsonparsing.ActorAdapter$1.onClick(ActorAdapter.java:76)
at android.view.View.performClick(View.java:4881)
at android.view.View$PerformClick.run(View.java:19592)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5756)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
任何人都可以建议我一个解决方案。以前我已经从Java类实现了这种类型的Starting Activity
。但在这种情况似乎与我不同。我正在寻找你的建议。
答案 0 :(得分:5)
这个问题是因为你正在从CustomArrayAdapter类开始一个活动,所以为了做到这一点,你必须在
之间添加一行。Intent i = new Intent(context, Details.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //add this line
context.startActivity(i);
您的代码将正常运行
答案 1 :(得分:0)
在调用时使用parent.getContext()
或set context param in constructor of adapter
并使用该引用
答案 2 :(得分:0)
使用
Intent intent=new Intent(...).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(i);
答案 3 :(得分:0)
有几种类型的背景。适配器不是使用Activity的上下文启动的(这很好),但只有活动的上下文可以在其他活动中进行午餐! 因此,在这种情况下,您必须将活动传递给适配器,并将其用作上下午午餐。
答案 4 :(得分:0)
尝试这样的事情......
将来自activty的上下文引用传递给CustomAdapter类..
public class CustomAdapter extends Adapter {
private Context context;
public CustomAdapter (Context context) {
this.context = context;
}
}
现在您可以使用此上下文启动活动。