I am currently trying to make a basic android app and trying to wrap my head around some crashes.
I've tried commenting out various parts of the code and compiling and running. As far as I can tell it seems to be when I have a Button and TextView both in a second activity. I am trying to make the app to transfer text put in by the user in the main activity to a second activity. (after a button is pushed). I am trying to get the second activity to have a button that returns to the first activity and also show the text that was put in from the main activity.
Here is the code for the main activity:
package com.example.brochura;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
import java.lang.String;
public class Main extends Activity
{
public final static String EXTRA_MESSAGE = "com.example.brochura.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendMessage(View view)
{
Intent intent = new Intent(this, Deployed.class);
EditText editText = (EditText)findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
And here's the code for the second activity:
package com.example.brochura;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.widget.TextView;
public class Deployed extends Activity
{
public final static String EXTRA_MESSAGE = "com.example.brochura.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.return_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
finish();
}
});
Intent intent = getIntent();
String message = intent.getStringExtra(Main.EXTRA_MESSAGE);
setContentView(R.layout.deployed);
TextView textView = (TextView) findViewById(R.id.message_text);
textView.setText(message);
}
}
The crash happens when I try to go to the second activity (deployed)
Here is the xml for the Main and Deployed classes:
(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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:id="@+id/deploy_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
(deployed.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/return_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
android:text="Return"
/>
<TextView
android:id="@+id/message_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="100dp"
/>
</LinearLayout>
Thanks in advance and I am fairly new to mobile development so any pointers that anyone can give me would also be greatly appreciated.
--- UPDATE: Here is the logcat
I/ActivityManager( 1180): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.brochura/.Main} from pid 1552
I/ActivityManager( 1180): Start proc com.example.brochura for activity com.example.brochura/.Main: pid=2013 uid=10052 gids={50052}
I/ActivityManager( 1180): Displayed com.example.brochura/.Main: +321ms
E/AndroidRuntime( 2013): Process: com.example.brochura, PID: 2013
E/AndroidRuntime( 2013): at com.example.brochura.Main.sendMessage(Main.java:36)
W/ActivityManager( 1180): Force finishing activity com.example.brochura/.Main
W/ActivityManager( 1180): Activity pause timeout for ActivityRecord{b131c5f0 u0 com.example.brochura/.Main t7 f}
W/InputDispatcher( 1180): channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)' ~ Consumer closed input channel or an error occurred. events=0x9
E/InputDispatcher( 1180): channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)' ~ Channel is unrecoverably broken and will be disposed!
I/ActivityManager( 1180): Process com.example.brochura (pid 2013) has died.
W/InputDispatcher( 1180): Attempted to unregister already unregistered input channel 'b149a920 com.example.brochura/com.example.brochura.Main (server)'
I/WindowState( 1180): WIN DEATH: Window{b149a920 u0 com.example.brochura/com.example.brochura.Main}
No comments:
Post a Comment