Reputation: 63
I have written the code as on developer.android.com, and I have some problems.
First, in the MainActivity.java file, it marks "message" with yellow line under it.
Second, I have no errors in code, but when I'm running the app, the text box says "false" and the button has "false" caption. When I press the button, nothing happens. Can please someone help me?
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = null;
private static final String message = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@+string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
//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);
}
}
MyFirstApp Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
Thanks for the helpers I will really appriciate it.
right now this is the code:
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Upvotes: 2
Views: 3315
Reputation: 1441
in your coding start activity before message variable assing so only it does not show message so using following way
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
startActivity(intent);
Upvotes: 6
Reputation: 39406
a final object (a String in your case) cannot be reaffected. In your case, it means it will always be null. Which is not understood by Eclipse, hence the warning.
Then there are several sequencing issues in your app. You are fetching the value from the EditText after sending the intent, which doesn't work.
Then, there is shadowing. You are declaring a message String that is hiding the private variable.
As for the texts, there is no need for + in @+string/edit_message. + means that the element is not explicitely declared. A text must be explicitly declared, otherwise its value makes no sense.
Also, the EXTRA key you use is arbitrary (i.e. you can give it any value you like), but null is not a good value.
Upvotes: 1
Reputation: 159784
You are setting your message to a local copy of that variable. You should assign it to your static variable instead. The warning is as a result of the local variable not being used after being declared. To fix you can remove the String
keyword:
message = editText.getText().toString();
Upvotes: 3