Working with new instance of a class in Android - 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

Related

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

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"

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

App crashing when on nexus7 but works on emulator android

I have created a main screen to have 3 buttons. And one of them is to open another page that displays data from my database in a textview. It works perfectly on the emulator on my laptop but when I copy the files to my Nexus 7 that button crashes the application. All other buttons work fine.
here's the code, very basic:
viewFlare = (Button)findViewById(R.id.bViewFlare);
viewFlare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openViewFlare = new Intent("com.example.project.SQLFLAREVIEW");
startActivity(openViewFlare);
}
});
it calls this page:
public class SQLFlareView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlflareview);
TextView textView = (TextView) findViewById(R.id.tvSQLFlareinfo);
Calms info = new Calms(this);
info.open();
//System.out.print("THIS IS THIS OPEN");
String data = info.getFlareData();
//System.out.print("ABOUT TO CLOSE");
info.close();
textView.setText(data);
}
}
If anyone would have any ideas I would be very appreciative, I have banged my head about this problem for a while.
Thanks in advance
Change this:
Intent openViewFlare = new Intent("com.example.project.SQLFLAREVIEW");
To this:
Intent openViewFlare = new Intent(name_of_current_class.this, SQLFLAREVIEW.class");

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