不是封闭类/新意图无法解析构造函数

时间:2016-10-27 23:10:03

标签: java android-studio android-intent onclicklistener

感谢您查看我的问题!

我有一个相对简单的循环,它会为我添加到我的Layout的每个ImageView添加一个onClickListener,但是当我尝试向它添加一个新的Intent时,它会给我一个以下错误:

    String[] imageURLs = imageURLsString.split("/");
    for (int i = 0; i < imageURLs.length; i++){
        ImageView image = new ImageView(this);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
        image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
        final String imageURL = ".../images/" + imageURLs[i];
        Picasso.with(this).load(imageURL).into(image);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                intent = new Intent(this, ImageActivity.class);
                intent.putExtra("URL", imageURL);
                startActivity(intent);
            }
        });
        linearLayout.addView(image);
    }

将导致“无法解析构造函数'意图....”错误。 因此,当我寻找解决方案时,人们建议将“this”更改为“MainActivity.this”,但是......

    String[] imageURLs = imageURLsString.split("/");
    for (int i = 0; i < imageURLs.length; i++){
        ImageView image = new ImageView(this);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
        image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
        final String imageURL = ".../images/" + imageURLs[i];
        Picasso.with(this).load(imageURL).into(image);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent;
                intent = new Intent(MainActivity.this, ImageActivity.class);
                intent.putExtra("URL", imageURL);
                startActivity(intent);
            }
        });
        linearLayout.addView(image);
    }

导致:com.myName.appName.MainActivity不是封闭类

以下是ImageActivity类:

public class ImageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        String URL = getIntent().getExtras().getString("URL");

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.getLayoutParams().height = (int) (imageView.getLayoutParams().width * (Float.valueOf(String.valueOf(1.41))));
        Picasso.with(this).load(URL).into(imageView);
    }

奇怪的是,我在创建应用程序的过程中做了很多新的Intent,但我似乎无法解决这个问题。我在这里缺少什么?

来自此处的完整代码

public class DetailsActivity extends AppCompatActivity {


    TextView TVWeChatIdValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        String dataString = getIntent().getExtras().getString("dataString");
        String[] itemData = dataString.split(",");
        String category = itemData[0];
        Log.e("DetailsActivity", category);
        String itemID = itemData[1];
        Log.e("DetailsActivity", itemID);
        String imageURLsString = itemData[2];
        Log.e("DetailsActivity", imageURLsString);
        String description = itemData[3];
        Log.e("DetailsActivity", description);
        String price = itemData[4];
        Log.e("DetailsActivity", price);

        String itemCode = category + itemID;

        TextView textView;
        textView = (TextView) findViewById(R.id.Title);
        textView.setText(getResources().getString(R.string.app_name));
        textView = (TextView) findViewById(R.id.ItemReferralValue);
        textView.setText(itemCode);
        textView = (TextView) findViewById(R.id.ItemPriceValue);
        textView.setText(price);

        TVWeChatIdValue = (TextView) findViewById(R.id.WeChatIDValue);
        new DatabaseTask(this, "details", "GETWECHATID").execute();

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ItemDetailsContent);

        String[] imageURLs = imageURLsString.split("/");
        for (int i = 0; i < imageURLs.length; i++){
            ImageView image = new ImageView(this);
            image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
            image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
            final String imageURL = ".../images/" + imageURLs[i];
            Picasso.with(this).load(imageURL).into(image);

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent;
                    intent = new Intent(this, ImageActivity.class);
                    intent.putExtra("URL", imageURL);
                    startActivity(intent);
                }
            });
            linearLayout.addView(image);
        }

        ImageView imageView = (ImageView) findViewById(R.id.ItemDetailsDescription);
        String imageURL = ".../images/" + description;
        Picasso.with(this).load(imageURL).into(imageView);

    }

    public void SetWeChatId(String mValue) {
        TVWeChatIdValue.setText(mValue);
    }

    public int getPx(int dimensionDp) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dimensionDp * density + 0.5f);
    }
}

工作代码

public class MainActivity extends AppCompatActivity {

    TextView TVWeChatIdValue;
    LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView;
        textView = (TextView) findViewById(R.id.Title);
        textView.setText(getResources().getString(R.string.app_name));
        textView = (TextView) findViewById(R.id.SubTitle);
        textView.setText(getResources().getString(R.string.slogan));

        textView = (TextView) findViewById(R.id.MoreForWomen);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenCatalog("women");
            }
        });
        textView = (TextView) findViewById(R.id.MoreForMen);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenCatalog("men");
            }
        });
        textView = (TextView) findViewById(R.id.MoreForKids);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenCatalog("kids");
            }
        });
    }

    public void GetData() {
            TVWeChatIdValue = (TextView) findViewById(R.id.WeChatIDValue);
            new DatabaseTask(this, "main", "GETWECHATID").execute();

            linearLayout = (LinearLayout) findViewById(R.id.NewItemsForWomenContent);
            linearLayout.removeAllViewsInLayout();
            new DatabaseTask(this, linearLayout, "WOMEN", "GETNEWESTITEMS").execute();
            linearLayout = (LinearLayout) findViewById(R.id.NewItemsForMenContent);
            linearLayout.removeAllViewsInLayout();
            new DatabaseTask(this, linearLayout, "MEN", "GETNEWESTITEMS").execute();
            linearLayout = (LinearLayout) findViewById(R.id.NewItemsForKidsContent);
            linearLayout.removeAllViewsInLayout();
            new DatabaseTask(this, linearLayout, "KIDS", "GETNEWESTITEMS").execute();
    }

    public void SetWeChatId(String mValue) {
        TVWeChatIdValue.setText(mValue);
    }

    public void ProcessNewItems(LinearLayout mLinearLayout, final String mCategory, final HashMap<Integer, Item> mItems){
        for (int i = 0; i < mItems.size(); i++) {
            ImageView image = new ImageView(this);
            image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(96),getPx(128)));
            image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
            String imageURL = ".../images/" + mItems.get(i).getImageURLs()[0];
            Picasso.with(this).load(imageURL).into(image);

            mLinearLayout.addView(image);
            final int index = i;
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShowItemDetails(mCategory, mItems.get(index));
                }
            });
        }
    }

    void ShowItemDetails(String mCategory, Item mItem){
        Intent intent = new Intent(this, DetailsActivity.class);
        intent.putExtra("dataString", mItem.GetDataString());
        Log.e("CatalogActivity", mItem.GetDataString());
        startActivity(intent);
    }

    void OpenCatalog(String mCategory){
        //show catalog
        Intent intent = new Intent(this, CatalogActivity.class);
        intent.putExtra("category", mCategory);
        startActivity(intent);
    }

    public int getPx(int dimensionDp) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dimensionDp * density + 0.5f);
    }

    @Override
    public void onResume() {
        super.onResume();

        GetData();
    }

}

2 个答案:

答案 0 :(得分:1)

你有这个块

image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent;
        intent = new Intent(MainActivity.this, ImageActivity.class);
        intent.putExtra("URL", imageURL);
        startActivity(intent);
    }
});

MainActivity.this更改为设置Activity的{​​{1}}子类。在你的情况下,这将是OnClickListener。基本上,封闭活动仅表示正在定义DetailsActivity.this的类文件的名称(也是Activity类的名称),此Activity封装了对{的调用{1}}您要传递一个匿名内部类,它在您的Activity中被括起来,因此包含活动的名称。希望有意义

答案 1 :(得分:1)

您需要使用包含创建Activity的代码的OnClickListener子类的名称。在许多示例中,这是MainActivity。但是,在您的代码中似乎并非如此。由于您位于名为DetailsActivity的类中,因此请使用该名称:

Intent intent = new Intent(DetailsActivity.this, ImageActivity.class);

从我在此处看到的代码中,我强烈建议您了解ListViewRecyclerView。它们有点复杂,但是一旦你理解了它们,它们就会为你做很多工作,就像你在这里做的那样。首先,它们比您自己的代码更有效,因为它们只创建尽可能多的视图。

让我们更仔细地看看你的另一个例子:

public class MainActivity extends AppCompatActivity { 
    // ...
    void ShowItemDetails(String mCategory, Item mItem){
        Intent intent = new Intent(this, DetailsActivity.class);
        intent.putExtra("dataString", mItem.GetDataString());
        startActivity(intent);
     }
}

请注意,ShowItemDetails()位于MainActivity类中。因此this指的是该类的一个实例,可以在需要Context的任何地方使用。 (有关这方面的更多详细信息,您需要阅读有关继承和多态的内容。)

另一方面,您的原始代码在Intent的匿名子类中的方法中创建OnClickListener,该方法不能用作Context。但是,匿名内部类位于DetailsActivity内的方法中,该方法继承自Context。要访问此外部类的实例,您必须使用DetailsActivity.this

有关详细信息,您应该了解特殊的this引用和内部类。