sdieters sdieters - 1 month ago 11
Java Question

is not an enclosing class / new Intent Cannot Resolve Constructor

Thanks for checking out my question!

I have a relativly ismple loop which will add a onClickListener to each ImageView I add to my Layout, however when trying to add a new Intent to it, it gives me one of the following errors:

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);
}


Will result in the "Cannot resolve Constructor 'Intent ...." error.
So when I was looking for a solution, people suggested to change "this" to "MainActivity.this", but...

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);
}


resulted into: com.myName.appName.MainActivity is not an enclosing class

Here is the ImageActivity class:

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);
}


The weird thing is that I have done numerous new Intent's during my play time with creating apps, but I cant seem to solve this one. What am I missing here?

FULL CODE FROM HERE

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);
}
}


WORKING CODE

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();
}

}

Answer

You need to use the name of the Activity subclass which contains the code that creates the OnClickListener. In many examples, this is MainActivity. However, this doesn't seem to be the case in your code. Since you are in a class named DetailsActivity then use that name:

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

From what I can see of your code here, I strongly suggest that you learn about ListView and RecyclerView. They are a bit complex, but once you understand them, they do much of the work for you similar to what you are trying to do here. For one thing, they are more efficient than your own code because they only create as many views as are visible.

Comments