19

我想从此基本适配器开始一个新活动。

public class EfficientAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<ComptePost> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader; 
    public  Boolean isActusAstuce;
    public static int flag = 0, counter=0;

    private Context context;

    public EfficientAdapter(Context context) {
        this.context = context;     
    }

    NVirementEmmeteur main;

    int num = 0;

    ViewHolder holder;
    static String src;

    public EfficientAdapter(Activity a, ArrayList<ComptePost> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //  imageLoader = new ImageLoader(activity.getApplicationContext());
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public EfficientAdapter(NVirementEmmeteur m) {
        main = m;
    }

    @Override
    public int getCount() {
        return data.toArray().length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView one;
        public TextView two;
        public TextView three;
        public ImageView image;
        public RelativeLayout relative_layout;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        holder.relative_layout.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.getContext().startActivity(new Intent(context, NVirementEmmeteur.class)); 
            }
        });
        return vi;
    }
}

我试过了

context.startActivity(new Intent(context, NVirementEmmeteur.class));

v.getContext().startActivity(new Intent(context, NVirementEmmeteur.class));

但它强制关闭我的应用程序。

onclicklistener()意图应该在来自列表适配器的内部启动。有人能告诉我如何从我的efficientadapter.class 启动一个意图吗?

这是我的 logcat 输出:

04-11 10:07:50.878: E/AndroidRuntime(11179): 致命异常: main 04-11 10:07:50.878: E/AndroidRuntime(11179): java.lang.NullPointerException 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android.content.ComponentName.(ComponentName.java:75) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android.content.Intent.(Intent.java:2863 ) 04-11 10:07:50.878: E/AndroidRuntime(11179): at.adapter.EfficientAdapter$1.onClick(EfficientAdapter.java:141) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android .view.View.performClick(View.java:2538) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android.view.View$PerformClick.run(View.java:9152) 04-11 10 :07:50.878: E/AndroidRuntime(11179): 在 android.os.Handler.handleCallback(Handler.java:587) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android.os.Handler。调度消息(处理程序.java:92) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 android.os.Looper.loop(Looper.java:130) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在android.app.ActivityThread.main(ActivityThread.java:3687) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-11 10:07 :50.878: E/AndroidRuntime(11179): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 04 -11 10:07:50.878:E/AndroidRuntime(11179):在 dalvik.system.NativeStart.main(本机方法)878: E/AndroidRuntime(11179): 在 android.app.ActivityThread.main(ActivityThread.java:3687) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 java.lang.reflect.Method.invokeNative (本机方法) 04-11 10:07:50.878: E/AndroidRuntime(11179): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 10:07:50.878: E/AndroidRuntime( 11179): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 com.android.internal.os.ZygoteInit .main(ZygoteInit.java:600) 04-11 10:07:50.878: E/AndroidRuntime(11179): at dalvik.system.NativeStart.main(Native Method)878: E/AndroidRuntime(11179): 在 android.app.ActivityThread.main(ActivityThread.java:3687) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 java.lang.reflect.Method.invokeNative (本机方法) 04-11 10:07:50.878: E/AndroidRuntime(11179): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 10:07:50.878: E/AndroidRuntime( 11179): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 04-11 10:07:50.878: E/AndroidRuntime(11179): 在 com.android.internal.os.ZygoteInit .main(ZygoteInit.java:600) 04-11 10:07:50.878: E/AndroidRuntime(11179): at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime(11179): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal.os.ZygoteInit $MethodAndArgsCaller.run(ZygoteInit.java:842) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 04-11 10 :07:50.878:E / AndroidRuntime(11179):在dalvik.system.NativeStart.main(本机方法)E/AndroidRuntime(11179): at java.lang.reflect.Method.invoke(Method.java:507) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal.os.ZygoteInit $MethodAndArgsCaller.run(ZygoteInit.java:842) 04-11 10:07:50.878: E/AndroidRuntime(11179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 04-11 10 :07:50.878:E / AndroidRuntime(11179):在dalvik.system.NativeStart.main(本机方法)

4

9 回答 9

24

您已经在构造函数中传递了活动上下文,因此您也可以使用;

activity.startActivity(new Intent(activity, NVirementEmmeteur.class));

检查这里是示例代码,您会知道该怎么做:

setadapter 像:adapter = new MyArrayAdapter(MainActivity.this, COUNTRIES);

适配器代码:

package com.example.testapp;

import com.example.main.util.testActivity;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

class MyArrayAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Context mcon;
    private String[] COUNTRIES_;

    public MyArrayAdapter(Context con, String[] countries) {
        // TODO Auto-generated constructor stub
        mcon = con;
        COUNTRIES_ = countries;
        mInflater = LayoutInflater.from(con);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return COUNTRIES_.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListContent holder;
        View v = convertView;
        if (v == null) {
            v = mInflater.inflate(R.layout.my_spinner_style, null);
            holder = new ListContent();
            holder.line = (LinearLayout) v.findViewById(R.id.line_);
            holder.name = (TextView) v.findViewById(R.id.textView1);
            holder.name1 = (TextView) v.findViewById(R.id.textView2);
            holder.name2 = (ImageView) v.findViewById(R.id.imageView1);

            v.setTag(holder);
        } else {

            holder = (ListContent) v.getTag();
        }

        holder.name.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
        holder.name1.setText("" + Html.fromHtml("" + COUNTRIES_[position]));

        holder.line.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mcon.startActivity(new Intent(mcon, testActivity.class));
            }
        });

        return v;
    }

}

class ListContent {

    TextView name;
    TextView name1;
    ImageView name2;
    LinearLayout line;

}

编辑:

如果你使用这个构造函数:那么list.setadapter(new EfficientAdapter(myactivity.this));

public EfficientAdapter(Context context) {
          this.context = context;     
     }

然后使用:context.startActivity(new Intent(context, NVirementEmmeteur.class));


如果你使用这个构造器list.setadapter(new EfficientAdapter(myactivity.this, ComptePostarray));

public EfficientAdapter(Activity a, ArrayList<ComptePost> d) {

        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //  imageLoader = new ImageLoader(activity.getApplicationContext());
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

然后使用activity.startActivity(new Intent(activity, NVirementEmmeteur.class));

希望你能理解......

于 2013-04-11T06:04:55.423 回答
9

我之前遇到过这个问题并尝试了上述所有建议,但唯一帮助我从采用者那里开始活动的是@Md 提出的解决方案。萨杰杜尔·卡里姆。

我修改了代码并像这样使用

Intent intent = new Intent(context, NesneTani.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
于 2016-09-02T19:17:12.937 回答
8

你也可以这样做,这mContext是你的BaseAdpter Context对象

 Intent ieventreport = new Intent(mContext,Your.class);   
 mContext.startActivity(ieventreport);

改变你的构造函数,初始化

 public EfficientAdapter(Context context, ArrayList<ComptePost> d){
}
于 2013-04-11T06:08:13.303 回答
3

用这个替换 onClick 方法它可能有效,我没有尝试过:

@Override
public void onClick(View v) {    
    v.getContext().startActivity(new Intent(v.getContext(), NVirementEmmeteur.class)); 
}
于 2019-05-19T13:58:58.560 回答
2

通过构造函数传递上下文,然后只使用这行代码:

Intent intent=new Intent(context.getApplicationContext(), YourActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
  context.getApplicationContext().startActivity(intent);

谢谢 :)

于 2016-03-12T19:25:19.020 回答
1

为你需要打开的类设置一个意图。在清单文件中。例如:

<activity android:name=".openingclass"
android:label="@string/app_name">
 <intent-filter>
 <action android:name="android.intent.action.OPEN" />
 <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
 </activity>

然后

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
           Intent i=new Intent("android.intent.action.OPEN");
           StartActivity(i);
                  }
于 2013-04-11T06:09:38.037 回答
0

从活动传递活动上下文并在调用时使用相同的上下文Intent.

从活动:

yourview.setAdapter(new EfficientAdapter(this));

在适配器内:

 private Context mContext;


  public EfficientAdapter(Context c) {
        mContext = c;

    }

现在mContext用来调用 Intent。

startActivity(new Intent(mContext, NVirementEmmeteur.class));
于 2013-04-11T06:11:20.747 回答
0

在 mainactivity 中的 listview 上设置 clickListener 从那里开始意图

    lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{ 
    Intent intent = new Intent(getActivity,Test.class);
startActivity(intent);
}
});
于 2013-04-11T06:13:44.970 回答
0

您可以使用 getContext() 从 View v 或适配器类中的任何其他创建的视图中简单地获取上下文。

// TODO Auto-generated method stub
v.getContext().startActivity(new Intent(v.getContext(), NVirementEmmeteur.class)); 

于 2021-03-29T13:38:57.750 回答