我目前正在尝试制作一个基本的Android应用程序并试图绕过一些崩溃。
我已经尝试评论代码的各个部分并编译和运行。据我所知,似乎是在第二个活动中我有一个Button和TextView。我正在尝试使应用程序将主要活动中用户输入的文本传输到第二个活动。 (按下按钮后)。我试图让第二个活动有一个按钮返回到第一个活动,并显示从主活动放入的文本。
以下是主要活动的代码:
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);
}
}
这是第二项活动的代码:
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);
}
}
当我尝试进入第二个活动(已部署)
时发生崩溃以下是Main和Deployed类的xml:
(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: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://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="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>
提前感谢,我对移动开发还是一个新手,所以任何人都可以给我的任何指示也将非常感激。
---更新: 这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.brochura"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19"
/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Deployed" />
</application>
</manifest>
这是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}
答案 0 :(得分:0)
put this line in first actvity.
Intent i=new Intent(First.this,second.class);
i.putExtra("strin", "HI this is my text");
startActivity(i);
and put this line in second activity
Intent i=getIntent();
String str=i.getStringExtra("strin");
declare both activity in manifest