App crashes when i press the button - java

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

Related

Android imageview id not resolved,

<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

Null pointer on not null index? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Like in subject. I got null pointer, how it is possible ?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
You just forgot to do findViewById to your Controls in OnCreate()
button2 =(Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.btnID);
textView2 =(textView) findViewById(R.id.textView2);
Sample code
#Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.mybtnID);
button2 = (Button) findViewById(R.id.button2);
textView2 = (textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
size = list.size();
textView2.setText(size);
}
});
}
You are missing
findViewById for all your widget
i.e.
Button button, button2;
TextView textView, textView2;
button = findViewById(R.id.id_of_button_1); // same for button2,textview,textview2
You can initialize with findById(R.id.myBtn) like everyone is saying. Or programatically like button = new Button(this);
Try this
You forgot to add findViewById
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
Try this are missing findViewById for all your widget
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
Sample Code
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}

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

Why setText() function doesn't work?

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

OnSeekBarChangeListener always evaluates to null in Android

I have an android program that I am making with 4 seekbars, and 4 textviews to accompany each seekbar. Right now, I am trying to make each textview's contents equal to the integer progress of the seekbars. This is my code as it stands, and I have tried many ways to properly implement the OnSeekBarChangeListener, but LogCat still shows that it is null when sb1-sb4 sets their OnSeekBarChangeListener to OnSeekBarProgress. Please help me identify my problem :(
public class TippopotamusActivity extends Activity {
SeekBar sb1;
SeekBar sb2;
SeekBar sb3;
SeekBar sb4;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
OnSeekBarChangeListener OnSeekBarProgress =
new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar s, int progress, boolean touch){
if(s.getId() == R.id.seekBar1)
{
tv1.setText(progress);
}
else if(s.getId() == R.id.seekBar2)
{
tv2.setText(progress);
}
else if(s.getId() == R.id.seekBar3)
{
tv3.setText(progress);
}
else
{
tv4.setText(progress);
}
}
public void onStartTrackingTouch(SeekBar s){
}
public void onStopTrackingTouch(SeekBar s){
}
};
Move your setContentView(R.layout.main); before initializing your variables. Your onCreate method should look like this
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
}

Categories