Errors in first Android application - java

so I've been going through a beginner's guide for creating Android Applications (http://developer.android.com/training/basics/firstapp/index.html). It has worked fine except for one step which I keep getting errors at.
The MainActivity.java does not work for me. I am getting errors of all kinds on 3 different places.
This is what my code looks like:
package com.fredde.myfirstapp;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
import android.app.Activity;enter code here
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
So I'm hoping that someone who has gone through this guide can help me out, or just someone who can see what's wrong despite not having done this particular project. Thanks in advance!
Go easy on me, I'm a complete beginner.

import statements should be out of the class.
package com.fredde.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

For your main activity:
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
the following has to be defined in strings.xml under res->values->
<resources>
...
<string name="title_activity_display_message">My Message</string>
</resources>
for R can not be resolved:
make sure you have defined edit_message as an id for edit view in your xml file like the following:
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
this should solve your problem

Ok, so after copying G V's code there is only one error left: On this
line ditText editText = (EditText) findViewById(R.id.edit_message); it
says "R cannot be resolved to a variable"
Clean the project and then build it again.

You need to call onCreate and inside, setCustomView, to inflate a layout to that activity.
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCustomView(R.layout.yourLayoutXMLfile);
}
If you do not write this the app doesn't know where to find objects like EditText, etc.

I just compiled the following and didn't get any errors.
This is what I did, under onCreate method..
setCustomView(R.layout.file.xml);
for this create a file called file.xml under res->layout->file.xml.
I assigned an id for Edit view in the xml like :`
<EditText
android:id="#+id/edit_mssg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/msg"/>
I am sure you have a java file called DisplayMesageActivity.java
package com.fredde.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCustomView(R.layout.file.xml);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_mssg);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

Related

Why can't I get any other output from my program through setOnClickListener?

I didn't get any response from my Android App through setOnClickListener, and I didn't get any output on Clicking TextView. What should I do now?
My code is the following:
XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#3ba8e7"
android:id="#+id/login_reg"
/>
Java:
package com.example.rehman.userloginregister;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class user_register extends AppCompatActivity {
TextView register_reg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_register);
register_reg = (TextView) findViewById(R.id.login_reg);
register_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
}
});
}
}
If you want to start another activity after clicking on your textview (register_reg), you have to create an intent.
Assumming that your layout 'R.layout.activity_main' is associated to the activity class 'MainActivity.java':
public void onClick(View view) {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
Firstable create a new Activity to your activity_layout and than on your click listener write this code
Intent intentions = new Intent(UserRegister.this,YourSecondActivity.class);
startActivity(intent);
And please a class start with an uppercase letter

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

First Android App. edit_message cannot be resolved or is not a field

I did a lot of googling but other people don't seem to have the same problem as I.
The full error is :
Multiple markers at this line.
-Line breakpoint:MainActivity - sendMessage(View) I don't know what this means
-edit_message caanot be resolved or is not a field
Here is the line of code that's getting the error.
EditText editText = (EditText) findViewById(R.id.edit_message);
Here is my activity_main.xml
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message"/>
Here is the full MainActivty.java
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I think you are using the wrong layout. You put the field in activity_main.xml, but you are setting the content view to activity_list_item.
Remove the:
import android.R;
line.
This tells Android to use the internal resources R.java file, and not your project's. Removing it will fix the issue. You may have to go to Project -> Clean after doing this.
Move:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
}
to DisplayMessageActivity.java

Email intent in constant loop

Ive got my email intent working well as in it posts emails. Im now getting an issue where once I click 'send email' on my custom template, which sets the 'To', 'subject' and 'message' fields of the GMail template it loads fine but then when i click 'send' on the GMail template it loops back to the custom template. As shown:
This is my custom made email template:
Once 'send email' is clicked then the Gmail template is loaded:
Once the 'Send' button of the GMail template is cliked it loops back to the custom template and they loop between each other continuously. Hoping someone can give me an idea of how I can stop this looping!
Here is my code with my intent within the 'onClick':
package com.example.flybase2;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ContactsEmail extends Activity implements OnClickListener{
String emailPassed;
String emailAdd;
String emailSub;
String emailMess;
EditText setEmailAddress;
EditText setEmailSubject;
EditText setEmailMessage;
Button btnSendEmail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emaillayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
emailPassed = extras.getString("passedEmailAdd");
}
setEmailAddress = (EditText) findViewById (R.id.inputEmailAddress);
setEmailAddress.setText(emailPassed);
setEmailSubject = (EditText) findViewById (R.id.inputEmailSubject);
setEmailMessage = (EditText) findViewById (R.id.inputEmailMessage);
btnSendEmail = (Button)findViewById(R.id.btnSendEmail);
btnSendEmail.setOnClickListener(this);
}
#Override
public void onClick(View sendEmailClick) {
emailAdd = setEmailAddress.getText().toString();
emailSub = setEmailSubject.getText().toString();
emailMess = setEmailMessage.getText().toString();
Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("plain/text");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd});
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub);
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess);
startActivity(Intent.createChooser(sendEmailIntent, "Send mail..."));
}
}
It looks like you simply want to call finish() at the end of onClick(). This will close ContactsEmail allowing the user to return to a more useful Activity from the GMail app.

Button to vibrate makes android app force close

Im just making a simple app that will vibrate when the button is clicked, but for some reason when i click the button the app says it unexpectedly stopped and needed to force close, below is the source code to the main java file and i have used the android vibrate permission in my manifest. can someone tell me why every time I click the vibrate button it gives me the error that it unexpectedly stopped?
package com.test;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.EditText;
public class Main extends Activity {
public final static String EXTRA_MESSAGE = "com.test.MESSAGE";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/* Called when the user clicks the button */
public void sendMessage(View view) {
// do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void vibrateMe() {
Vibrator vibrate = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrate.vibrate(500);
}
public void stopVibrating(Vibrator vibrate) {
vibrate.cancel();
}
}
You have to change your vibrateMe() to vibrateMe(View v) if you use android:onClick="vibrateMe"
For instance, if you specify android:onClick="sayHello", you must
declare a public void sayHello(View v) method of your context
(typically, your Activity).
Check the developer page
public void stopVibrating(Vibrator vibrate) {
vibrate.cancel();
}
remove this and then check.

Categories