Error: unknown class: 'message' - how to fix that? - android

I just started teaching myself android development and I am running into an error on this basic hello world application.
I tried searching but everything seems like chinese language to me.
here is the tutorial i was following: https://developer.android.com/training/basics/firstapp/starting-activity
and here is a screenshot of my error:
[I will include the code below][1]: https://i.stack.imgur.com/Xm1tg.png
<code>
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);//on this line i get an error under "message"
</code>

Move that code in onCreate() method like :
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}

You need to move the following code inside onCreate()
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);//on this line i get an error under "message"

Related

Android app crashes when retrieving int from an intent in another activity

I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );

Android call intent getting wrong telephone number

I'm trying to make the button (R.id.buttonring) call any phone number entered in the textview called tvTelefon. However, when i click that button, my phone is trying to call "w4126848130,511290,549" instead of the phone number entered in the textview. Any ideas how come? I don't receive any error messages so I'm clueless! Thanks!
public class Activity2 extends Activity {
public static final String SPARAD_DATA = "sparadData";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
SharedPreferences sp = getSharedPreferences(SPARAD_DATA, Context.MODE_PRIVATE);
TextView tvRubrik = (TextView)findViewById(R.id.textViewrubrik);
tvRubrik.setText(sp.getString("rubrik", "Rubrik saknas"));
TextView tvNamn = (TextView)findViewById(R.id.textViewnamn);
tvNamn.setText(sp.getString("namn", "Namn saknas"));
TextView tvText = (TextView)findViewById(R.id.textViewtext);
tvText.setText(sp.getString("text", "Text saknas"));
final TextView tvTelefon = (TextView)findViewById(R.id.textViewtelefon);
tvTelefon.setText(sp.getString("telefon", "Telefon saknas"));
TextView tvPris = (TextView)findViewById(R.id.textViewpris);
tvPris.setText(sp.getString("pris", "Pris saknas"));
Button r = (Button)findViewById(R.id.buttonring);
r.setOnClickListener (new View.OnClickListener() {
Intent call = new Intent(Intent.ACTION_DIAL);
#Override
public void onClick(View v){
call.setData(Uri.parse("tel:" +tvTelefon));
startActivity(call);
}
});
}
}
Slight change might help you:
call.setData(Uri.parse("tel:" +tvTelefon));
It should be:
call.setData(Uri.parse("tel:" +tvTelefon.getText().toString()));
you are passing an object reference, get the string you put into it.
Actually, how I understand your posted code you can just take the phone number straight from your preferences. Try this:
call.setData(Uri.parse("tel:" + sp.getString("telefon", "")));
Instead of:
call.setData(Uri.parse("tel:" +tvTelefon));

Working with new instance of a class in Android

Totally new and starting out just working through the app tutorials. I wanted to run an algorithm to test out how it runs. I have it working all perfectly in java but am not sure how to create a class instance correctly in android.
public class DisplayMessageActivity extends Activity {
myAlgorithm myAlg = new myAlgorithm();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
However as soon as run the program I get a force close. I take it this isn't the correct way to do it with Android?
TIA

Displaying text from another activity on a TextView

I am trying to use putExtra() and getExtra() to send String Data from one activity to another, such that the retrieved string is to be displayed on a TextView and when running.
When i run the program i get a classCastException on onCreate() method.
I am new to android so any assistance will be appreciated.
Here is my sample code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales);
//TextView
TextView model1 = (TextView)findViewById(R.id.model1);
TextView model2 = (TextView)findViewById(R.id.model2);
TextView model3 = (TextView)findViewById(R.id.model3);
//Bundle
Bundle bundle = getIntent().getExtras();
String Mod1 = bundle.getString("model1");
String Mod2 = bundle.getString("model2");
String Mod3 = bundle.getString("model3");
//setting values
model1.setText(Mod1);
model2.setText(Mod2);
model3.setText(Mod3);
}
Your widgets model1,model2,model3 are either not TextView or you do not pass strings to the intent that you pass to the new Activity. Also you could try to clean your projects, maybe your R.java file is messed up. You could also paste the LogCat or tell us which is the line that gives you the ClassCastException.

Passing value from one window to the another in android

I want to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.
I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
EditText BMI_weight;
public String weight;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_main);
Button submit =(Button)findViewById(R.id.BMI_submit);
BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
weight = BMI_weight.getText().toString();
// create a bundle
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("wt", weight);
// add bundle to the intent
Intent intent = new Intent(v.getContext(), BMI_Result.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
);
Second Activity :
public class BMI_Result extends Activity
{
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//get the bundle
Bundle bundle = getIntent().getExtras();
// extract the data
String weight = bundle.getString("wt");
Weight.setText(weight);
}
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");

Categories

Resources