I've created a timer and placed it in its own separate class. For some reason when I try to run it it crashes with the following error:
Java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I think it because its trying to update a textview from a class other than Mainactvity but I cant figure out how to get it to work.
Here's the code:
MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button resetButton;
TextView textTimer;
int elapsedTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elapsedTime = 60;
textTimer = (TextView) findViewById(R.id.textTimer);
resetButton = (Button) findViewById(R.id.resetButton);
final Button resetbttn = (Button) findViewById(R.id.resetButton);
resetbttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CountUp mycountup = new CountUp();
mycountup.count();
}
});
}
}
Timer Class:
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Mat on 24-Feb-17.
*/
public class CountUp extends MainActivity{
TextView textTimer;
Button resetButton;
int elapsedTime;
Handler h;
int RATE = 1000;
MainActivity mainact = new MainActivity();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textTimer = (TextView) findViewById(R.id.textTimer);
resetButton = (Button) findViewById(R.id.resetButton);
h = new Handler();
count();
final Button resetbttn = (Button) findViewById(R.id.resetButton);
resetbttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reset();
}
});
}
public void reset(){
elapsedTime = 60000;
}
public void count(){
elapsedTime--;
textTimer.setText(String.valueOf(elapsedTime));
h.postDelayed(r,RATE);
final String endcomparison = textTimer.getText().toString();
if (endcomparison.equals("0")){
reset();
}
}
private Runnable r = new Runnable() {
#Override
public void run() {
count();
}
};
}
findViewById() method actually looks for view in layout file.
when you set layout for activity or fragment using
setContentView(R.layout.activity_main);
findViewById() find those views in that layout. it can't find random objects on the fly.
so in second activity set layout first or create sub class in first activity and then try to update data in textview.
Your are just instantiating an activity, which it's onCreate will never get called.
CountUp mycountup = new CountUp();
mycountup.count(); // view never created so it's null
To load the views you need to use a method like startActivity.
If I am understanding you correctly, if you are extending CountUp from MainActivity then in your manifest, you'd set CountUp as your launching activity. Then using Java inheritance, you don't need to re-find the views, because in CountUp we are calling super.onCreate(savedInstanceState), which calls the MainActivity's onCreate.
public class MainActivity extends AppCompatActivity {
Button resetButton;
TextView textTimer;
int elapsedTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elapsedTime = 60;
textTimer = (TextView) findViewById(R.id.textTimer);
resetButton = (Button) findViewById(R.id.resetButton);
}
}
public class CountUp extends MainActivity{
Handler h;
int RATE = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// can access this since inheritance
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reset();
}
});
h = new Handler();
count();
}
//...
public void count(){
elapsedTime--;
// inheritance
textTimer.setText(String.valueOf(elapsedTime));
h.postDelayed(r,RATE);
final String endcomparison = textTimer.getText().toString();
if (endcomparison.equals("0")){
reset();
}
}
//...
}
Related
I am new to android studios and was tasked with creating an app programmatically. Depending on which button is pressed, the color description is displayed. I have a feeling it has something to do with the layout I created. Every time I run the program the app crashes and I cannot figure out why. Below is my code:
package com.example.hw3ex2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout layout;
LinearLayout.LayoutParams layoutParams;
String plumDescription = getResources().getString(R.string.plum_is);
String blueDescription = getResources().getString(R.string.blue_is);
String goldDescription = getResources().getString(R.string.gold_is);
String descriptionString;
TextView tv;
Button btnPlum;
Button btnBlue;
Button btnGold;
private int button_color = getResources().getColor(R.color.button_color);
private int background_color=getResources().getColor(R.color.background_color);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGUIByCode();
}
private void buildGUIByCode() {
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create textview
tv = new TextView(this);
tv.setLayoutParams(params);
layout.addView(tv);
//create buttons and add them to layout
btnPlum = new Button(this);
btnPlum.setTag("plum");
btnPlum.setText("Plum");
btnPlum.setLayoutParams(params);
layout.addView(btnPlum);
btnPlum.setOnClickListener(this);
btnPlum.setBackgroundColor(button_color);
btnBlue = new Button(this);
btnBlue.setTag("blue");
btnBlue.setText("Blue");
btnBlue.setLayoutParams(params);
layout.addView(btnBlue);
btnBlue.setOnClickListener(this);
btnBlue.setBackgroundColor(button_color);
btnGold = new Button(this);
btnGold.setTag("gold");
btnGold.setText("Gold");
btnGold.setLayoutParams(params);
layout.addView(btnGold);
btnGold.setOnClickListener(this);
btnGold.setBackgroundColor(button_color);
setContentView(layout);
}
#Override
public void onClick(View view) {
Object tag = view.getTag();
if ("plum".equals(tag)) {//get plum_is string and display in textView
tv.setText(plumDescription);
} else if ("blue".equals(tag)) {//get blue_is string and display in textview
tv.setText(blueDescription);
} else if ("gold".equals(tag)) {//get gold_is string and display in textview
tv.setText(goldDescription);
}
System.out.println(descriptionString);
}
}
I see that you're invoking getResources() before onCreate,
you need to move all of those initialization that uses getResources() within onCreate
public class MainActivity ... {
String plumDescription; // don't initialize here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plumDescription = getResources().getString(R.string.plum_is); // initialize after onCreate
}
}
I am new to Android Studio and trying to get used to it. I have tried to make a trivial app where the user could press a button and a random number would be generated. However, when I used the variables in the code it gave me "unknown class 'get'" and "unknown class 'random'" errors. Code is as follows:
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button RollButton = findViewById(id.RollButton);
final TextView resultsTextView = findViewById(id.ResultsTV);
final SeekBar seekBar = findViewById(id.seekBar);
RollButton.setOnClickListener(new onClickListener()
{
Random rnd = new Random(10);
private int random = rnd.nextInt(seekBar.getProgress());
CharSequence get = resultsTextView.getText();
get = String.valueOf(random);
});
}
}
Your OnClickListener looks wrong and you can't use private in this case. So try this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button RollButton = findViewById(id.RollButton);
final TextView resultsTextView = findViewById(id.ResultsTV);
final SeekBar seekBar = findViewById(id.seekBar);
RollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random rnd = new Random(10);
int random = rnd.nextInt(seekBar.getProgress());
CharSequence get = resultsTextView.getText();
get = String.valueOf(random);
}
});
}
}
I'm trying to figure out how progress bars work so i made a simple app where you have a plus and minus button which change the progress of the progress Bar. No errors in Android studio, but it crashes when launched on my phone (Honor 5X)
package net.gamepickle.rcap_new;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
int progressStatus = 50;
ProgressBar progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void run(){
Button plus = (Button) findViewById(R.id.happiness_plus);
Button minus = (Button) findViewById(R.id.happiness_minus);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=100){
progressStatus += 1;
progressBar.setProgress(progressStatus);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(progressStatus!=0){
progressStatus -= 1;
progressBar.setProgress(progressStatus);
}
}
});
}
}
Please add your stack trace.
It seems that you are calling
ProgressBar progressBar = (ProgressBar)findViewById(R.id.happiness_progress);
on the class level.
You need to split it.
Declare: ProgressBar progressBar;
and on the onCreate function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.happiness_progress);
}
I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.
The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.
Sounds.java
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
}
You can't randomly create a method like you're doing with onCreate1, it's never executed by you or by your Activity.
This is all you need:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(this);
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
You aren't assigning the onclick listener inside the actual OnCreate. This should work:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}