Android Studio & Java build issue - java

I am trying to finish off the first tutorial in the Android Studio documentation, but I can't even seem to do that.
I keep getting a:
Error:(17, 60) error: cannot find symbol variable EXTRA_MESSAGE
Here is the code to my DisplayMessageActivity.java file:
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
}
The tutorial I am trying to follow is here:
https://developer.android.com/training/basics/firstapp/starting-activity.html

There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = <Some Message String>. And this error will be resolved

There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = .

Related

Data passing between two activities repetitively

I'm a beginner in android studio and i want to write a code with 3 activities.
1st is for starting the app.2nd is for showing an English word and 3rd is for showing the English word and a description of it in two texts.
I want to transfer data of English word itself and its description and the number of the word to show the words one by one.
I wrote the code with help of tutorial clips but it ain't work and in 3rd activity shows nothing.
these are my codes:
main activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view)
{
Intent i = new Intent(this,ActivityOne.class);
startActivity(i);
}
}
ActivityOne
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityOne extends AppCompatActivity {
int a=0;
String E , P;
private Button show;
private TextView Eword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Eword = (TextView) findViewById(R.id.Eword);
show = (Button) findViewById(R.id.show);
}
public void show(View view)
{
Intent intent = new Intent(this,ActivityTwo.class);
startActivity(intent);
int a = getIntent().getIntExtra("counter",0);
Eword.setText(Eng[a]);
E = Eword.getText().toString();
P = Fa[a];
a++;
intent.putExtra("counter",a);
intent.putExtra("EWord",E);
intent.putExtra("PWord",P);
}
private String[] Eng = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
private String[] Fa = {
"Abundance Description",
"Anxiety Description",
"Bruxism Description",
"Discipline Description",
"Drug Addiction Description"
};
}
ActivityTwo
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
TextView Eng , Fa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Eng = (TextView) findViewById(R.id.eng);
Fa = (TextView) findViewById(R.id.fa);
// recieve data from activity one
String EWord = getIntent().getStringExtra("EWord");
String PWord = getIntent().getStringExtra("PWord");
int a = getIntent().getIntExtra("counter",0);
Eng.setText(EWord);
Fa.setText(PWord);
}
public void iknow(View view)
{
Intent myIntent = new Intent(this,ActivityOne.class);
startActivity(myIntent);
int a = getIntent().getIntExtra("counter",0);
myIntent.putExtra("counter",a);
}
public void idknow(View view)
{
Intent myIntentTwo = new Intent(this,ActivityOne.class);
startActivity(myIntentTwo);
int a = getIntent().getIntExtra("counter",0);
myIntentTwo.putExtra("counter",a);
}
}
And it shows this result:
It is the third activity and it can clear the pretext that was set in design page but can not replace EWord or PWord
Can someone help me?????
(1) To get you started, you need to put your variables into the intent before you start the activity. (2). I don't think its a good idea to have activity one start two, which then can start one. It is better to just close second activity which then automatically returns to one. (3) To pass data back to the activity you are returning you need to startActivityForResult https://developer.android.com/training/basics/intents/result.html
activity one starts activity two using startActivityForResult. Activity Two returns an intent (which is the result intent) and activity one recieves this in onActivityResult.
check out this: Android: how to make an activity return results to the activity which calls it?

Android Studio: playing with Intents and sending data between activities

I'm new to Android Studio and Java, currently I'm learning about intents and sending data/text from one activity to another. On the second activity I get "Cannot resolve symbol 'intent'" even though it's in onCreate field. What I'm trying to do is to have two text fields for first and last name, sending the text from first activity to the second one which will read it, and all done with onButtonClick method. I'm only trying to do the first name and the code looks like this MainAcitivty
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClicked(View view) {
EditText nameText = (EditText)findViewById(R.id.firstName);
TextView messageText = (TextView) findViewById(R.id.messageTextView);
if(nameText.getText().toString().trim().length() == 0){
messageText.setText("text here");
} else {
String textForSecondAcitivity = "your name is: " + nameText.getText();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
startActivity(intent);
`
The problem is the second Activity that gives me the error "Cannot resolve symbol 'intent'". And here is the code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent scIntent = getIntent();
String messageText = intent.getStringExtra("EXTRA_TEXT");
TextView messageView = (TextView)findViewById(R.id.textView);
messageView.setText(messageText);
}
It wont show the error here but it's int this line
String messageText = intent.getStringExtra("EXTRA_TEXT");
I apologize in advance because surely I messed up something bad, I'm a beginner and if you have some advice on what's the best way to do what I'm actually trying to accomplish feel free to tell me.
The app should look like this in the end 1.The first activity
1.The second one showing first name and last name
You have no local variable named intent
Replace it with scIntent
Here you are making it wrong
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
instead you use this
intent.putExtra("EXTRA_TEXT", textForSecondAcitivity);

Android Programming with Java, how to use R.id

studio to create an android app that adds a string from an editText with an id of input and displays it into textView with an id of output. But
EditText input = EditText(findViewById(R.id.input));
and
TextView output = TextView(findViewById(R.id.output));
doesnt work as it says Method call expected. Any kind of help would be great, thankyou.
package com.example.toshb.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Model model;
public MainActivity() {model = new Model();}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void processInput(View view)
{
EditText input = EditText(findViewById(R.id.input));
TextView output = TextView(findViewById(R.id.output));
model.addString(editText.getText().toString());
output.setText(model.getList());
input.setText("");
}
}
You need to add proper declaration of EditText And TextView . Before write a code please have a look here:
https://developer.android.com/index.html
EditText input = (EditText)findViewById(R.id.input);
TextView output =(TextView)findViewById(R.id.output);
model.addString(input.getText().toString());

Build failed, wrong class imported?

I am trying to build a very simple Android app, with just instantiating one class, but for some reason it fails and doesn't build, I am probably making some fundamental mistake but am unable to figure out what it is.
package algon.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import anroid.widget.TextView;
import android.graphics.drawable.Drawable;
import android.content.Context;
public class AlgonActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView t = new TextView(Context context);
setContentView(R.layout.main);
}
}
It just doesn't want to instantiate the TextView class, I can't understand why actually.
Thank you for response in advance
TextView t = new TextView(Context context);
should be:
TextView t = new TextView(this);

Syntax error setOnClickListener?

ok if you didnt see my previous question I asked how 2 Command button to import text to textview from edittext using Scanner? Here is what I have done:
I keep geting this error
"Syntax error on token
"setOnClickListener",
VariableDeclaratorId expected after
this token"
what am I missing or doing wrong?
package test.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.Scanner;
import android.R.layout;
public class test extends Activity {
Scanner what = (new Scanner(System.in));
private int addbtn;
Button btn = (Button) findViewById(addbtn);
btn.setOnClickListener = (new OnClickListener() {
public void onClick(View v) {
int txtbox;
EditText txt = (EditText) findViewById(txtbox);
int tv1;
TextView txt1 = (TextView) findViewById(tv1);
txt.setText( txt.getText().toString() );}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
You're missing a closing brace } on the new OnClickListener block.
Also you shouldn't be attempting Button btn = (Button) findViewById(addbtn); before the onCreate(...) method has called setContentView(...).
On top of that, addbtn isn't a valid resource id.
Use findViewById() method before setContentView(...).
On top of which your closing brackets look messed up. What's matching the ( before the new onClickListener?

Categories