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);
}
}
}}
Related
<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
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);
I'm new to programming and have been racking my brain for the past couple of hours trying to resolve the issue. I know it's something stupid. I am trying to get each button to play a sound. Any help is greatly appreciated.
(ADDED "import android.view.View.OnClickListener;", TURNED MY PROBLEMS GREEN UNTIL I CLOSED IT OUT WITH ";" BUT THEN THE IMPORT GRAYED OUT.)
public class PocketParent extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer stopMP = MediaPlayer.create(this, R.raw.btmg);
final MediaPlayer dropMP = MediaPlayer.create(this, R.raw.knock);
final MediaPlayer popMP = MediaPlayer.create(this, R.raw.oldsoulwr);
final MediaPlayer noMP = MediaPlayer.create(this, R.raw.smpr);
final MediaPlayer yeahMP = MediaPlayer.create(this, R.raw.upbeaty);
ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);
// (HERE IS WHERE MY PROBLEM LIES)
// *OnClickListener is abstract, cannot be instantiated.*
// (IT ALSO SAYS IT'S EXPECTING A "," OR " ' " RIGHT BEFORE "new",
// but that doesn't work either.)
playstop.setOnClickListener(new View.OnClickListener()
playdrop.setOnClickListener(new View.OnClickListener()
playpop.setOnClickListener(new View.OnClickListener()
playno.setOnClickListener(new View.OnClickListener()
playyeah.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopMP.start();
dropMP.start();
popMP.start();
noMP.start();
yeahMP.start();
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
}
....
}
}
You just can't do this:
playstop.setOnClickListener(new View.OnClickListener()
playdrop.setOnClickListener(new View.OnClickListener()
playpop.setOnClickListener(new View.OnClickListener()
....
because this means in java, you are passing to playstop method many arguments with no comma spliting,
It would make more sense syntaxically to do
playstop.setOnClickListener(new View.OnClickListener());
playdrop.setOnClickListener(new View.OnClickListener());
playpop.setOnClickListener(new View.OnClickListener());
....
But you can't do this either because View.OnClickListener is an interface
And creating an object of the View.OnClickListener just make no sense...
you need to instead implement anonymously that interface on all that buttons like you did it with the playYeah button
playyeah.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
....
}
});
playstop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
....
}
});
playdrop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
....
}
});
Etc etc
Try Change
ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);
to
ImageButton playstop = (ImageButton) findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) findViewById(R.id.imageButton5);
Hope It help you!!
My app crashes as soon as i press the button "Lets's Play".
I just started developing.. and its not even the text that makes the error, i tried only to do tv1.setText("test"); and it crashed too.
here is my code :
package com.example.adam.casinoadam;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
ImageView iv1;
ImageView iv2;
ImageView iv3;
TextView tv1;
Button btn1;
int x,y,z;
int coins=15;
#Override
protected void onCreate(Bundle savedInstanceState) {
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Start(View view) {
if(coins==0) {
Toast.makeText(this, "Sorry. Good Bye!", Toast.LENGTH_LONG).show();
finish();
}
coins=coins-1;
tv1.setText("Money: "+coins+" Coins");
x=1+(int)((5-1+1)*Math.random());
y=1+(int)((5-1+1)*Math.random());
z=1+(int)((5-1+1)*Math.random());
if(x==1)iv1.setImageResource(R.drawable.cherry);
if(x==2)iv1.setImageResource(R.drawable.tiltan);
if(x==3)iv1.setImageResource(R.drawable.bell);
if(y==1)iv2.setImageResource(R.drawable.cherry);
if(y==1)iv2.setImageResource(R.drawable.tiltan);
if(y==1)iv2.setImageResource(R.drawable.bell);
if(z==1)iv3.setImageResource(R.drawable.cherry);
if(z==1)iv3.setImageResource(R.drawable.tiltan);
if(z==1)iv3.setImageResource(R.drawable.bell);
if(x==y && y==z && z==x){
Toast.makeText(this,"You won !",Toast.LENGTH_LONG).show();
coins=coins+10;
}
}
}
Modify your code like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
}
findViewById must be after setContentView
You must define the views after setContentView(R.layout.activity_main);
You can't initialize the view objects before setContentView called in onCreate method. If you initialize the view objects it will get null values because the activity doesnot get it's layout file.
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
These 5 lines must be after these two lines in onCreate method:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
So, your code must be :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
}
Replace your code with this onCreate() First you have to setContentView(R.layout.activity_main) befor casting your Button and all.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
So at last your code look like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
}
Replace your onCreate with below code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);1
}
findViewById() returns a View if it exists in the layout you provided
in setContentView(), otherwise it returns null and that's what
happening to you.
If you setContentView(R.layout.activity_main);
and then call findViewById(R.id.iv1); it will return a View which
is your layout, But if you call findViewById(R.id.iv1); before
setContentView(R.layout.activity_main); it will return null since
there is no view attached to your activity.
You must try doing this because until the layout is defined by contentview you cant use (findviewbyid) function in the program
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);
}
can u tell me why this code is not working? The app stops as soon as I click on Image(Imageview1). Debugger points to tv.setText(x);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = (TextView)findViewById(R.id.textView1);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String x="You clicked on the image.";
tv.setText(x);
}
});
}
You need to get your UI elements after inflating your layout, otherwise findViewById returns null and hence the NPE is thrown when you try to set the text on your textView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /// I swap these two lines
final TextView tv = (TextView)findViewById(R.id.textView1); ///
ImageView img = (ImageView) findViewById(R.id.imageView1);