0

I know this has been asked before but the solutions here are not helping. My problem exists in the getView method at the bottom of my ImageAndTextAdapter class. I am getting the following error when I create a new intent:

"The Constructor Intent(new View.OnClickListener(){} Class ) is undefined."

MenuCells exists and is also in the manifest file.

ImageAndTextAdapter.java

public class ImageAndTextAdapter extends ArrayAdapter<String> {

private LayoutInflater mInflater;

private String[] mStrings;
private TypedArray mIcons;

private int mViewResourceId;

public ImageAndTextAdapter(Context ctx, int viewResourceId,
        String[] strings, TypedArray icons) {
    super(ctx, viewResourceId, strings);

    mInflater = (LayoutInflater)ctx.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    mStrings = strings;
    mIcons = icons;

    mViewResourceId = viewResourceId;
}

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

@Override
public String getItem(int position) {
    return mStrings[position];
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(mViewResourceId, null);

    ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
    iv.setImageDrawable(mIcons.getDrawable(position));

    TextView tv = (TextView)convertView.findViewById(R.id.option_text);
    tv.setText(mStrings[position]);

    tv.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

             ****THE ERROR GETS THROWN AT THIS LINE****
             Intent intent = new Intent(this, MenuCells.class);
             intent.putExtra("CELL_NAME", mStrings[position]);
                         startActivity(intent);


        }

    });

    return convertView;
}

}

MENU_CELLS.java

package com.laerdalsun;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MenuCells extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle extras = getIntent().getExtras();

    if(extras!=null) {
           String value = extras.getString("CELL_NAME");
    }

}

}

4

3 回答 3

1

这里this指的是OnClickListener但你需要一个Context. 创建一个成员变量并将其分配给Context您传递给您的构造函数

  public class ImageAndTextAdapter extends ArrayAdapter<String> {

     private LayoutInflater mInflater;

     private String[] mStrings;
     private TypedArray mIcons;

     private int mViewResourceId;
     Context c;          // create the variable

public ImageAndTextAdapter(Context ctx, int viewResourceId,
        String[] strings, TypedArray icons) {
    super(ctx, viewResourceId, strings);
    c = ctx;          // assign it here
    // other code

并使用Context您创建的变量

 public void onClick(View v) {

         ****THE ERROR GETS THROWN AT THIS LINE****
         Intent intent = new Intent(c, MenuCells.class);   // and use it here

你还需要改变

startActivity(intent);

c.startActivity(intent);

编辑

正如您评论中的错误所说

startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

所以添加那个标志,你的代码看起来更像

 Intent intent = new Intent(c, MenuCells.class);
 intent.putExtra("CELL_NAME", mStrings[position]);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   // this line
 c.startActivity(intent);
于 2013-11-07T19:24:48.923 回答
0

要创建您需要在上下文中传递的意图,错误表示您正在尝试传递一个OnClickListener,在您正在执行它的上下文中是this

稍微修改一下你的适配器

private Context context;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
        String[] strings, TypedArray icons) {
    super(ctx, viewResourceId, strings);

    mInflater = (LayoutInflater)ctx.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    mStrings = strings;
    mIcons = icons;
    context = ctx;
    mViewResourceId = viewResourceId;
}

只需将您的意图构造函数更改为此Intent intent = new Intent(context, MenuCells.class);

于 2013-11-07T19:25:17.850 回答
0
****THE ERROR GETS THROWN AT THIS LINE****
Intent intent = new Intent(this, MenuCells.class);
intent.putExtra("CELL_NAME", mStrings[position]);
startActivity(intent);

检查Intent构造函数,this使用实际上下文进行更改,
还需要startActivity(intent)在它自己的上下文上调用。

于 2013-11-07T19:24:37.753 回答