Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
想要在android中将文本从一个活动发送到另一个活动吗_Android_Xml - Fatal编程技术网

想要在android中将文本从一个活动发送到另一个活动吗

想要在android中将文本从一个活动发送到另一个活动吗,android,xml,Android,Xml,我曾尝试编写一个程序,用于将文本从主活动发送到另一个活动“DisplayMessageActivity”。但当我在安卓手机上安装该应用程序时,按下按钮后,它崩溃了,并显示一条消息“不幸的是,该应用程序已停止” 我的不同文件如下…(eclipse) MainActivity.java文件 包com.example.practice3 导入android.content.Intent; 导入android.os.Bundle; 导入android.support.v4.app.Fragment; 导

我曾尝试编写一个程序,用于将文本从主活动发送到另一个活动“DisplayMessageActivity”。但当我在安卓手机上安装该应用程序时,按下按钮后,它崩溃了,并显示一条消息“不幸的是,该应用程序已停止”

我的不同文件如下…(eclipse)

  • MainActivity.java文件

    包com.example.practice3

    导入android.content.Intent; 导入android.os.Bundle; 导入android.support.v4.app.Fragment; 导入android.support.v7.app.ActionBarActivity; 导入android.view.LayoutInflater; 导入android.view.Menu; 导入android.view.MenuItem; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.EditText; 公共类MainActivity扩展了ActionBarActivity{ 公共最终静态字符串EXTRA_MESSAGE=“com.example.myfirstapp.MESSAGE”

    }

  • 2.fragment_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>
    
    }

    4.fragment\u display\u message.xml文件

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.practice3.DisplayMessageActivity$PlaceholderFragment" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
    </RelativeLayout>
    
    
    
    5.Strings.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">My First App</string>
        <string name="edit_message">Enter a message</string>
        <string name="button_send">Send</string>
        <string name="action_settings">Settings</string>
        <string name="title_activity_main">MainActivity</string>
        <string name="title_activity_display_message">DisplayMessageActivity</string>
        <string name="hello_world">Hello world!</string>
    
    </resources>
    
    
    我的第一个应用程序
    输入消息
    邮寄
    设置
    主要活动
    显示消息活动
    你好,世界!
    
    只要检查一下,如果你复制了代码,它就会工作

    我建议你创建一个空白的活动,而不是一个带有片段的活动。同样,使用android更容易

    创建用于存储文本的意图:

    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);
    }  
    
    。。。下面是您刚刚开始的活动的
    onCreate
    方法:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // 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);
    

    }

    在DisplayMessageActivity.java文件中,注释下面的代码。 相应的xml文件中没有
    R.id.container


    您是否已在应用程序标记的AndroidManifest.xml中添加了DisplayMessageActivity,如:

    <activity android:label="@string/app_name" android:name=".DisplayMessageActivity"/>
    
    
    
    您能发布logcat输出吗?可能与您在清单中添加的活动名称重复吗?发布您的android清单先生,我已经在eclipse上构建了这个,还没有在emulator上尝试过,因为它花费的时间太长。没有错误。我已经直接将它加载到我的android手机上,然后安装了它。当我启动它时,第一个主要活动开始,然后显示一个按钮和一个文本字段,当我按下按钮时,应用程序崩溃并显示消息“不幸的是,应用程序已停止”先生,我已经在清单文件中添加了活动,先生,请帮助我,我刚刚开始学习。我建议您创建一个新的空白项目,你完全按照我给你的链接上说的去做,我也试过了,效果很好!先生,我实际上已经从您建议的这个链接上的教程开始了。但这不起作用,请帮帮我。
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // 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);
    
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    
    <activity android:label="@string/app_name" android:name=".DisplayMessageActivity"/>