Whenever I
seekBar.setMax(20);
seekBar.setProgress(10);
the app crashes. One or both of these crashes the app. (Are they deprecated?)
If I comment these two lines out and add the
setOnSeekBarChangeListener, then the listener will crash the app.
I'm not sure why, but I'm having issues using the Seekbar. Any help is super appreciated.
MainActivity.java
package com.example.test.timestableapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView timesTableListView = findViewById(R.id.listView);
SeekBar timesTablesSeekBar = findViewById(R.id.seekBar);
setContentView(R.layout.activity_main);
// timesTablesSeekBar.setMax(20);
// timesTablesSeekBar.setProgress(10);
timesTablesSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int min = 1;
int timesTableNumber;
if (i < min)
{
timesTableNumber = min;
}
else
{
timesTableNumber = i;
}
Log.i("Seekbar Value", Integer.toString(timesTableNumber));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
You forgot to define your view
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourview)
ListView timesTableListView = findViewById(R.id.listView);
SeekBar timesTablesSeekBar = findViewById(R.id.seekBar);
}
After your edit:
Here, you are trying to initialize ListView & SeekBar before inflating the layout:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView timesTableListView = findViewById(R.id.listView);
SeekBar timesTablesSeekBar = findViewById(R.id.seekBar);
setContentView(R.layout.activity_main);
Instead, inflate your layout first then initialize widgets or etc.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here, this is the solution.
ListView timesTableListView = findViewById(R.id.listView);
SeekBar timesTablesSeekBar = findViewById(R.id.seekBar);
// Here your other codes ...
}
You need to add
setContentView(R.layout.activity_main);
after
super.onCreate(savedInstanceState);
Tip to identify problem
Always try to read logs specially error logs in case of App Crashing.
If you cannot find solution for that problem don't forget to post error logs while posting your question.
Happy coding :)
Related
This is my code, it should print a random value (between 0 and 1) but it doesn't do that!
I don't know how to fix it! I tried multiple things, none of them are working out!
Here's the code:
package com.example.lode.coder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TextView;
import java.util.Random;
public class Coder extends AppCompatActivity {
TextView display;
String one;
boolean bl= true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coder);
display = (TextView) findViewById(R.id.text);
}
void all() {
while (bl) {
Random rand = new Random();
int n = rand.nextInt(2);
one = n + (String) display.getText();
display.setText(one);
}
}
}
You don't show where the all() method is called. If it's not called anywhere, that would explain why display never gets any text set.
But assuming that it is called somewhere, there's still a big problem: the all() method is an infinite loop. Until you return control to the framework, any changes you make to display won't show up on the screen. (In fact, your app will likely then be killed off by the framework when it notices that the app has become unresponsive.)
If you want to change the text continuously, look into using a Handler. You can create a Runnable that does the actual change and then reschedules itself to run again after a short time. Don't use a loop like you currently have in all().
Something like this would work:
public class Coder extends AppCompatActivity {
private static long UPDATE_INTERVAL = 500; // every half second -- adjust as needed
TextView display;
String one;
Handler handler;
Runnable updater;
Random rand = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coder);
display = (TextView) findViewById(R.id.text);
Handler handler = new Handler();
updater = new Runnable() {
#Override public void run() {
int n = rand.nextInt(2);
one = n + display.getText().toString();
display.setText(one);
handler.postDelayed(updater, UPDATE_INTERVAL);
}
}
}
#Override protected void onStart() { // or override onResume() instead
super.onStart();
startUpdates();
}
#Override protected void onStop() { // or override onPause() instead
super.onStop();
stopUpdates();
}
void startUpdates() {
handler.post(updater);
}
void stopUpdates() {
handler.removeCallbacks(updater);
}
}
you haven't called all() function anywhere and even if you do, the logic would still be wrong because bl always stays true and so the while loop is an infinite loop which never stops. Try this code:
public class Coder extends AppCompatActivity {
TextView display;
String one;
boolean bl= true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coder);
display = (TextView) findViewById(R.id.text);
all();
}
void all() {
Random rand = new Random();
int n = rand.nextInt(2);
one = n + display.getText().toString();
display.setText(one);
}
}
if you want the loop you need to set your bl variable to false at some point to stop the loop from going on infinitely.
"Variable is accessed within inner class Needs to be declared final" is the the error I get at first. So I change it to final. Once I change it to final I get a different error saying "Cannot assign value to final variable". I'm kind of stuck here on what to do here. Its giving me the error on int exam_grade
public class CalculateGradeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seekbar();
}
public void seekbar(){
SeekBar seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
final TextView text_view = (TextView) findViewById(R.id.percentageSeekbar1);
int exam_grade;
TextView text_view5 = (TextView) findViewById(R.id.numeric_grade_id_output);
text_view5.setText(String.valueOf(exam_grade));
int progress = seek_bar1.getProgress();
text_view.setText(String.valueOf(progress));
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
exam_grade = progress;
text_view.setText(String.valueOf(progress) + "%");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}//ends seekbar function
}
Hi don't declare your textview as final, put it in the class attributes instead.
public class CalculateGradeActivity extends Activity {
TextView text_view;
SeekBar seek_bar1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
text_view = (TextView) findViewById(R.id.percentageSeekbar1);
seekbar();
}
Do the same for all views(Declare them as attributes).
final means it can only be assigned once.
I feel sure that textview shouldn't be final.
anyway,
try putting the declarations of exam grade and TextView in your onCreate...
setContentView(R.layout.calculate_grade_view);
int exam_grade;
TextView text_view = (TextView) findViewById(R.id.percentageSeekbar1);
seekbar();
I am not sure if i need two classes or not, if I do I need to know how to pass data from one class to another and if not I need to know how to add an onClickListener to a class that already implements onSeekBarChangeListener.
Here's what I have so far:
package com.codeherenow.sicalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.SeekBar;
public class SICalculatorActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
private int years;
private TextView YT;
private SeekBar bar;
private EditText principal = (EditText) findViewById(R.id.PA_field);
private EditText interest = (EditText) findViewById(R.id.IR_field);
public EditText pvalue;
public EditText ivalue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
bar = (SeekBar)findViewById(R.id.seekBar);
bar.setOnSeekBarChangeListener(this);
pvalue = (EditText) principal.getText();
ivalue = (EditText) interest.getText();
}
#Override
public void onProgressChanged (SeekBar seekBar,int i, boolean b){
years = i;
YT = (TextView) findViewById(R.id.Years);
YT.setText(years + " Year(s)");
}
#Override
public void onStartTrackingTouch (SeekBar seekBar){
}
#Override
public void onStopTrackingTouch (SeekBar seekBar){
}
}
You can simply put a comma between all interfaces that you are implementing. In this case you'd need to have
public class SICalculatorActivity extends Activity
implements SeekBar.OnSeekBarChangeListener, View.OnClickListener{
You can continue using this method to implement any number of interfaces.
You can implement as many interfaces as you need:
public class SICalculatorActivity extends Activity implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
private int years;
private TextView YT;
private SeekBar bar;
private EditText principal;
private EditText interest;
public EditText pvalue;
public EditText ivalue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
//should init here to avoid NullPointerException
principal = (EditText) findViewById(R.id.PA_field);
interest = (EditText) findViewById(R.id.IR_field);
bar = (SeekBar)findViewById(R.id.seekBar);
bar.setOnSeekBarChangeListener(this);
// set click listener - someView.setOnClickListener(this);
pvalue = (EditText) principal.getText();
ivalue = (EditText) interest.getText();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
// case R.id.someViewId:
//handle click
// break;
}
}
#Override
public void onProgressChanged (SeekBar seekBar,int i, boolean b){
years = i;
YT = (TextView) findViewById(R.id.Years);
YT.setText(years + " Year(s)");
}
#Override
public void onStartTrackingTouch (SeekBar seekBar){
}
#Override
public void onStopTrackingTouch (SeekBar seekBar){
}
}
I have the following code to get information from a rating bar and put it into a TextView but it doesn't seem to be working:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ratingText = (TextView) findViewById(R.id.ratingText);
RatingBar rating=(RatingBar)findViewById(R.id.rating);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
public void onRatingChanged(RatingBar ratingBar,float rating, boolean fromUser){
ratingText.setText(""+this.rating.getRating());
}
At the second last line of code it is giving me the following error: "rating cannot be resolved or is not a field" and "ratingText cannot be resolved". I am wondering why I am getting these errors and how to fix them. Thanks.
a-) I think your problem is that you need that your class MainActivity implements OnRatingBarChangeListener:
public class MainActivity extends Activity implements OnRatingBarChangeListener {
}
b-) Another possible error is that you need to make sure that you do NOT have this in your imports:
import android.R;
but:
import your.application.packagename.R;
c-) Declare the TextView ratingText and the RatingBar rating outside of the method, as an attribute of the class.
Hope it helps!
Take a look at this for a solution.
r.string error "cannot be resolved or is not a field"
Main idea is to make sure your R reference is correct, and try by deleting the gen files and rebuild the project.
EDIT::
This was incorrect the problem was stated in another answer. by #Marv
It is because you create your ratingText and rating in the onCreate method. This means they are only available inside this method.
Try to do something like this:
private TextView ratingText = null;
private RatingBar rating = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingText = (TextView) findViewById(R.id.ratingText);
rating=(RatingBar)findViewById(R.id.rating);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
This way they are available in the entire class and you can use them in onRatingChanged.
try this.
public class Test extends Activity implements OnRatingBarChangeListener{
TextView ratingText;
RatingBar rating;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.info);
ratingText = (TextView) findViewById(R.id.textView_aaa);
rating=(RatingBar)findViewById(R.id.ratingBar1);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
ratingText.setText(""+this.rating.getRating());
}
}
When creating a titlebar with a button, which is common in all activities e.g. title bar created in tabactivities. how is it possible to reach the button in all of the sub activities??
public class tabActivity extends TabActivity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
This is here where i initialize my tabs, and the custom title with buttons..
And in this class i would like to reach the buttons in the custom title.:
public class About extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("heeey");
}
});
}
The listener doesnt work??
Hooow is this possible??
public class tabActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabactivity);
ImageView imag = (ImageView) findViewById(R.id.Position);
imag.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tabActivity.listener.onClick(v);
}
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
res.getDrawable(R.drawable.preferences)).setContent(
new Intent(this, Settings.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
res.getDrawable(R.drawable.newspaper)).setContent(
new Intent(this, About.class)));
}
public static void setListner(OnClickListener listener)
{
tabActivity.listner = listener;
}
main activity does not implements eventListener
public class About extends Activity implements OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
});
public void onResume()
{
tabActivity.setListener(this);
}
}
code goes like this. It's hard to explain
What is the purpose of that? The event controll is available in main activity.