未能在类中扩展StartActivity从Android中的BaseAdapter延伸?

时间:2014-02-19 17:06:34

标签: java android android-adapter

我正在尝试自定义listview哪个工作正常,现在我已经使用了这个部分中的baseAdapter的customclass扩展getView方法我在imageview中应用 onclickListener 并启动新的Activity这是崩溃的app  的

 public class CustomAdapterClass extends BaseAdapter
{
ArrayList<FatwaBean> fatwaArrayList;
Context context;

CustomAdapterClass(ArrayList<FatwaBean> data,Context c) {
    fatwaArrayList = data;
    context = c;
}
public int getCount() {
    // TODO Auto-generated method stub
    return fatwaArrayList.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return fatwaArrayList.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View v = convertView;
     if (v == null)
     {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.search_fatwa_row, null);
     }

      final ImageView image = (ImageView) v.findViewById(R.id.search_image_row);
       TextView dateTextView = (TextView)v.findViewById(R.id.search_date_row);

       FatwaBean fatwaBean= fatwaArrayList.get(position);
     //image.setImageResource(fatwaBean.getQuestionImage());
       image.setImageResource(R.drawable.app_icon);
      //image.setTag(fatwaBean.getFatwaTopicQuestionId());
       dateTextView.setText(fatwaBean.getFatwaDate());
       Log.i("fatwaBean",""+fatwaBean.getFatwaDate());
       Log.i("fatwaBean",""+fatwaBean.getFatwaImageName());
       Log.i("fatwaBean Topic Question Image Name",""+fatwaBean.getQuestionImage());

       image.setOnClickListener(new OnClickListener() {              
               public void onClick(View v) {
               // TODO Auto-generated method stub


               final int itemname= (int)fatwaArrayList.get(position).getFatwaTopicQuestionId();
               Log.i("clicked Fatwa Image Topic Id",""+fatwaArrayList.get(position).getFatwaTopicQuestionId());
               Intent intent = new Intent(context,FatwaZoomActivityImageView.class);
           //passing fatwa topic question Id to get FatwaImage in NextActivity

              intent.putExtra("questionTopicIdForFatwa",fatwaArrayList.get(position).getFatwaTopicQuestionId());
              intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
               context.startActivity(intent);

           }                        
       });
    return v;
}

logcat的

02-19 22:02:05.220: E/AndroidRuntime(578): FATAL EXCEPTION: main
02-19 22:02:05.220: E/AndroidRuntime(578): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.app.ContextImpl.startActivity(ContextImpl.java:803)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.jamia.binoria.CustomAdapterClass$1.onClick(CustomAdapterClass.java:73)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.view.View.performClick(View.java:3100)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.view.View$PerformClick.run(View.java:11644)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Handler.handleCallback(Handler.java:587)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.os.Looper.loop(Looper.java:126)
02-19 22:02:05.220: E/AndroidRuntime(578):  at android.app.ActivityThread.main(ActivityThread.java:3997)
02-19 22:02:05.220: E/AndroidRuntime(578):  at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:05.220: E/AndroidRuntime(578):  at java.lang.reflect.Method.invoke(Method.java:491)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-19 22:02:05.220: E/AndroidRuntime(578):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-19 22:02:05.220: E/AndroidRuntime(578):  at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

在startActivity()之前添加此项,如LogCat中所示。

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

答案 1 :(得分:0)

对我来说,logcat消息看起来非常简单:

  

从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?

如果您不想使用FLAG_ACTIVITY_NEW_TASK,那么您的适配器应该回调您的主Activity或Fragment以启动下一个Activity。

由于你正在调用intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);,我猜你真的不想要新的任务标志,并且回调父Activity / Fragment是你最好的选择。

答案 2 :(得分:0)

也许这应该是一个评论,但它有点长,所以......

context.startActivity(intent);

Context有一个StartActivity方法,Activity也有。您的context变量是Context类型,它指向一个Activity对象,但我不知道被调用的方法是属于Context还是属于Activity对象。

也许你可以尝试用Context context ??

替换你的成员变量Activity parentActivity

答案 3 :(得分:0)

您传递给Context构造函数的CustomAdapterClass不是Activity上下文,但有些不同,可能是Application。因此例外。请阅读this article以获取更多解释,了解不同Context的优势。

两种解决方案:

  1. 传入Activity上下文。这应该是首选方案。

  2. 按照其他一些答案的建议添加FLAG_ACTIVITY_NEW_TASK。请注意,这样做会影响活动后退堆栈导航的工作方式,而且不一定是您想要的。