Why does my code not work? - android

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

Related

Seekbar is crashing app

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

Set up TextView with method result

I'm a beginner with Java and am stuck in one place. I have a method like this in class 1
public String saySomething()
{
return "blabla";
}
Then in Main:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tw = (TextView) findViewById(R.id.text1);
tw.setText(saySomething());
}
}
I try set return from this method to TextView. Have no idea how but wanted to show what i want to do. How to do it properly?
In short, put saySomething() method in your Activity class or generate an instance of the class hosting the method and call it:
ClassName1 className = new ClassName1();
TextView tw = (TextView) findViewById(R.id.text1);
tw.setText(className.saySomething());

Why must be objects initialized in onCreate?

I have the following code:
public class MainActivity extends Activity {
TextView number=(TextView)findViewById(R.id.number2);
TextView number2=(TextView)findViewById(R.id.number2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number.setText("Text");
number.setText("Text");
}
followed by more code, but when I run it it crashes.
After doing that i tried to initialize TextViews in onCreate()
public class MainActivity extends Activity {
TextView number;
TextView number2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number=(TextView)findViewById(R.id.number);
number2=(TextView)findViewById(R.id.number2);
number.setText("Text");
number.setText("Text");
}
and it worked. Why must objects be initialized in onCreate?
Your activity won't have a Window until onCreate(). Attempting to find any views before the window is initialized will lead to NPE.
Additionally, attempting to find views before setContentView() will return nulls and so the return values are not good for anything.

What could possibly be causing this NPE in onCreate?

I am getting an NPE in onCreate of the following file (MySubActivity):
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTextView.setText(getResources().getString(R.string.myString));
}
}
MySuperActivity:
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
}
}
The strange thing is that I have never seen this crash while testing the app. The page works fine when I test it. However I got a crash report from Google notifying me of the crash. I cannot reproduce it, and I have no idea under what scenario this crash could happen. Seeing as how it works for me, the resource ids and string names etc. must be correct.
The only thing that came across my mind was that maybe the user had their phone set to a different language, so it couldn't properly pull the resources. However, there are default resources for all of them, and I tested changing the language of my emulator and it didn't crash. Any ideas?
Set your view in another method, like this:
public class MySubActivity extends MySuperActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void setView(){
myTextView.setText(getResources().getString(R.string.myString));
}
}
Edit: Don't call setView() in MySuperActivity
public class MySuperActivity extends Activity {
protected TextView myTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
myTextView = (TextView)findViewById(R.id.myTextViewid);
// Or, you can do this in a method called getView() if you like
}
}

Assigning Values to a TextView from a different class

TestaActivity.java
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
}
}
Print.java
public class Print {
public Print(Context tempContext) {
//I want to assign the value to the tvText from here
}
}
In the above example, as you can see I have set the text in tvText to "Sample". In the same way, I need to assign the textView1 ID with some value inside Print class, once it is created.
Please help me to figure out the way to do it.
If your class Print is instantiated when TestaActivity is on the screen, then you can get tvText reference, passing to Print in some way a TestaActivity reference.
Maybe you could pass it via constructor:
From TestaActivity you do:
Print print = new Print(this);
where this represents the instance of TestaActivity.
And then in your Print code you can do:
TextView tvText = (TextView)((TestaActivity)context.findViewById(R.id.textView1));
tvText.setText("Sample");
Another solution is provide an interface from TestaActivity, transparent for the outside, which manage your changes on the textview (or whatever).
Something like that:
private TextView tvText;
public void setTvText(String str){
tvText.setText( str );
}
And then in your Print class:
((TestaActivity)context).setTvText( "Sample" );
try as:
public class Print {
protected TestaActivity context;
public Print(Context tempContext) {
context = tempContext;
}
public void changetextViewtext(final String msg){
context.runOnUiThread(new Runnable() {
#Override
public void run() {
//assign the value to the tvText from here
context.tvText.setText("Hello Test");
}
});
}
}
and call changetextViewtext from Activity for Changeing TextView Text from Print Class
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
Print myPrint = new Print(this);
myPrint.changetextViewtext("Hello World !!!");
}
}
as your need!!!!:)
#imran - the solution is correct except that you would want to pass the TextView as an argument in the constructor or the method.
Harcoding TextView in a method is bad because you cannot be reuse it.

Categories

Resources