Android imageview id not resolved, - java

<ImageView
android:id="#+id/ivPopup"
android:layout_width="wrap_content"
android:layout_height="35dp"
app:srcCompat="#drawable/logout"
android:layout_marginStart="144dp"
android:layout_alignParentTop="true"`enter code here`
android:layout_toEndOf="#+id/tvProfileName"
android:layout_marginTop="2dp"
/>
Here is the java code to refer the ImageView
final ImageView ivPopup = (ImageView) findViewById(R.id.ivPopup);
For some reason, the android cannot resolve the ivPopup in R.id.ivPopup
Main Activity
public class HomePage extends AppCompatActivity implements View.OnClickListener {
private static final String TAG ="Menu";
private FirebaseAuth mAuth;
private ImageView ivPopup;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
TextView tvProfile = (TextView) findViewById(R.id.tvProfile);
tvProfile.setOnClickListener(this);
Button btnNotes = (Button) findViewById(R.id.btnNotes);
btnNotes.setOnClickListener(this);
Button btnTimeTable = (Button) findViewById(R.id.btnTimeTable);
btnTimeTable.setOnClickListener(this);
Button btnClass = (Button) findViewById(R.id.btnClass);
btnClass.setOnClickListener(this);
TextView tvProfileName = (TextView) findViewById(R.id.tvProfileName);
// Bundle extras =getIntent().getExtras();
// String user1 = extras.getString("username");
mAuth = FirebaseAuth.getInstance();
ivPopup = (ImageView) findViewById(R.id.ivPopup);
ivPopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(HomePage.this,ivPopup);
popupMenu.getMenuInflater().inflate(R.menu.menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(HomePage.this,"" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}

Clean code
Rebuild code
Try to Invalidate Caches & Restart.
if It's still not working then change your id from ivPopup to something else in XML as well as JAVA

Related

How to detect a click on the checkbox within a ListView when the its focusable is set to false

I am working on an application where I need to create a list. The list contains 2 textViews and 1 checkbox. The list has to delete the row which detects a click on it. But the click will only be detected when the checkbox's focusability is set to false. But the aim of the application is to also delete the row when the respective checkbox is checked.
But the onItemClickListener does not detect the click on the checkbox.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ArrayList<ListData> ListData = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Starting");
final ListDataAdapter adapter = new ListDataAdapter(this ,R.layout.list_layout,ListData );
Button button= (Button) findViewById(R.id.Button);
CheckBox checkBox =(CheckBox)findViewById(R.id.checkbox);
final ListView listView = (ListView)findViewById(R.id.ListView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView textView1 = (TextView) findViewById(R.id.TextView1);
TextView textView2 = (TextView) findViewById(R.id.TextView2);
EditText editText1 = (EditText) findViewById(R.id.EditText1);
EditText editText2 = (EditText) findViewById(R.id.EditText2);
if (editText1.getText() != null && editText2!=null) {
String title = editText1.getText().toString();
String description = editText2.getText().toString();
textView1.setText(title);
textView2.setText(description);
ListData data = new ListData(title, description);
ListData.add(data);
saveArrayList();
Log.i(TAG, "display:" + ListData);
adapter.notifyDataSetChanged();
editText1.setText("");
editText2.setText("");
listView.setAdapter(adapter);
}
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemClick: tapped");
ListData.remove(i);
saveArrayList();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
return true;
}
});
}
}
The XML file is
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="90"
android:focusable="false"
android:focusedByDefault="false" />

Android-Picasso java.lang.IllegalArgumentException: Target must not be null. when the image view is in a custom layout for a alertdialog

I have java.lang.IllegalArgumentException: Target must not be null. when I try to download an image and put it in the imageView By picasso library. let me show you my code:
public class ads_show extends AppCompatActivity {
private Toolbar toolbar;
private ImageView img_view1;
private HashMap<String, Object> hmap;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads_show);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
img = (ImageView) findViewById(R.id.ads_show_img);
TextView title = (TextView) findViewById(R.id.title_ads_show);
TextView desc = (TextView) findViewById(R.id.desc_ads_show);
TextView seller = (TextView) findViewById(R.id.seller_ads_show);
TextView phone = (TextView) findViewById(R.id.phone_ads_show);
TextView email = (TextView) findViewById(R.id.mail_ads_show);
TextView date = (TextView) findViewById(R.id.date_ads_show);
Bundle ads = getIntent().getExtras();
hmap = (HashMap<String, Object>) ads.get("ads");
Picasso.with(this).load((String) hmap.get("image_path"))
.resize(300, 150)
.into(img);
title.setText((String) hmap.get("title"));
desc.setText((String) hmap.get("description"));
seller.setText((String) hmap.get("seller"));
phone.setText((String) hmap.get("phone"));
email.setText((String) hmap.get("email"));
date.setText((String) hmap.get("date"));
}
public void onFullScreenClick(View v) {
try {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.fullscreen_image_show,null);
img_view1 = (ImageView) findViewById(R.id.img_fullscreen);
Picasso.with(this).load((String)hmap.get("image_path")).into(img_view1);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(layout);
alert.setCancelable(true);
alert.setNeutralButton("exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
} catch (Exception e) {
Log.i("app_error", "Full screen" + e.toString());
}
}
public void onBtnBackClick(View v) {
finish();
}
}
My problem is with onFullScreenClick method. and the image view in the onCreate method works fine.
You are forget to bind ImageView like ...
change in this line ...
img_view1 = (ImageView)layout.findViewById(R.id.img_fullscreen);

Issue with a method of animation in an Android project - Android studio

I'm testing a small animation example on Android studio. On the last method 'toggle' I'm getting an error Cannot resolve symbol views. Can someone help me to fix this issue?
public class MainActivity extends AppCompatActivity {
private ViewGroup viewGroup;
private TextView textView;
private ImageView imageView, imageView2, imageView3,imageView4,imageView7;
private Button button;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(ViewGroup) findViewById(R.id.viewGroup);
textView= (TextView) findViewById(R.id.textView);
textView.setText("It's animating");
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView7= (ImageView) findViewById(R.id.imageView7);
button = (Button)findViewById(R.id.clickme);
button.setText("Done");
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Explode());
toggle(textView, imageView, imageView2, imageView3, imageView4, imageView7);
}
});
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
}}

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

Crash when i try to update TextView

I'm trying to do an calculator app (school project), so i need a TextView for displaying the numbers. Because it's my first java project i wanted for the beginning to make a counter which displays how many times the button was clicked(just to learn how to work with TextViews). My problem is that after the first update of the TextView(when it is displaying "1") when i press the button for the second time it gives me a crash.
My code :
MainActivity.java
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
ImageButton button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
tv.setText(number.toString());
}
});
}
...
content_main.xml
...
<TextView
android:layout_width="172dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/no1"
android:layout_gravity="left|top"
android:editable="true" />
...
Modify your code to this:
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
//i would use this instead of the above
private int number = 0;
private TextView tv;
private ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.no1);
button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
tv.setText(String.valueOf(number));
}
});
}
...

Categories