Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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
Java 正在启动另一个活动-活动未显示我的消息_Java_Android - Fatal编程技术网

Java 正在启动另一个活动-活动未显示我的消息

Java 正在启动另一个活动-活动未显示我的消息,java,android,Java,Android,我是android开发新手,但有python和java方面的经验,所以我决定制作应用程序,因此我从developer.android.com开始了教程 我在构建你的第一个应用>开始另一个活动部分 我顺利完成了最后一步,但当我按下发送按钮时,我输入的消息没有显示在创建的第二个活动中 ()如果您单击该链接查看教程,并向下滚动到底部,您应该会看到您键入的消息会弹出,并且字体会更大。 当我键入任何内容时,我发现的消息都是小字体,默认为“Hello world!” 这是我的MyActivity.java

我是android开发新手,但有python和java方面的经验,所以我决定制作应用程序,因此我从developer.android.com开始了教程

我在构建你的第一个应用>开始另一个活动部分

我顺利完成了最后一步,但当我按下发送按钮时,我输入的消息没有显示在创建的第二个活动中

()如果您单击该链接查看教程,并向下滚动到底部,您应该会看到您键入的消息会弹出,并且字体会更大。 当我键入任何内容时,我发现的消息都是小字体,默认为“Hello world!”

这是我的MyActivity.java

    package com.example.android.myfirstapp;

import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/* Called when the user clicks the Send button */
public void SendMessage(View view) //View must be the parameter of onClick, View is what is pressed.
{
    // Do something in response
    Intent intent = new Intent(this, DisplayMessageActivity.class); //A Context as its first parameter (this is used because the Activity class is a subclass of Context)
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
package com.example.android.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.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);

    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

下面是DisplayMessageActivity.java

    package com.example.android.myfirstapp;

import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/* Called when the user clicks the Send button */
public void SendMessage(View view) //View must be the parameter of onClick, View is what is pressed.
{
    // Do something in response
    Intent intent = new Intent(this, DisplayMessageActivity.class); //A Context as its first parameter (this is used because the Activity class is a subclass of Context)
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
package com.example.android.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.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);

    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我不确定你们是否需要AndroidManifest.xml、activity\u my.xml或strings.xml,但安全总比抱歉好

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        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=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
    </activity>
</application>

</manifest>
<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"
    />
<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</string>

</resources>

活动_my.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        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=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
    </activity>
</application>

</manifest>
<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"
    />
<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</string>

</resources>

strings.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        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=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message" >
    </activity>
</application>

</manifest>
<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"
    />
<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</string>

</resources>

我的第一个应用程序!
输入消息!
发送
设置
主要活动
显示消息活动
你好
我很抱歉有些东西没有正确缩进,如果他们没有正确缩进,当我复制粘贴到这里

如果你能帮助我,我会非常感激,这样我就可以继续成为一名android开发者了

同样,该应用程序是一个简单的输入消息和发送,看到你的消息弹出较大的字体大小在另一个活动。虽然我的屏幕上只显示默认的小字体“Hello world!”,但当我按send时,新活动会弹出

谢谢您的时间,很抱歉发了这么长的帖子

首先,更换

intent.putExtra(EXTRA_MESSAGE, message);

在您的
MyActivity.java中

作为
putExtra()
的第一个参数,应该是一个字符串名,该名称将在应用程序中进一步使用。请参考[文档](,android.os.Bundle))

现在,在您的
DisplayMessageActivity.java

编写
onCreate()
,如下所示

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set your parent view
    setContentView(R.layout.disp_msg_layout);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra("EXTRA_MESSAGE");

    // Get the reference to the TextView and update it's content
    TextView textView = (TextView)findViewById(R.id.my_text_view);
    textView.setText(message);
}
并在布局中声明一个名为
disp_msg_layout
的xml文件,它应该是

<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="40sp"
           android:id="@+id/my_text_view"/>

首先,更换

intent.putExtra(EXTRA_MESSAGE, message);

在您的
MyActivity.java中

作为
putExtra()
的第一个参数,应该是一个字符串名,该名称将在应用程序中进一步使用。请参考[文档](,android.os.Bundle))

现在,在您的
DisplayMessageActivity.java

编写
onCreate()
,如下所示

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set your parent view
    setContentView(R.layout.disp_msg_layout);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra("EXTRA_MESSAGE");

    // Get the reference to the TextView and update it's content
    TextView textView = (TextView)findViewById(R.id.my_text_view);
    textView.setText(message);
}
并在布局中声明一个名为
disp_msg_layout
的xml文件,它应该是

<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="40sp"
           android:id="@+id/my_text_view"/>


不可能。您没有在代码的任何地方使用该
hello\u world
键。尝试搜索它的使用位置(右键单击“查找用法”)以查看它在第二个屏幕上的显示方式。@Lal该方法是从XML布局声明(
onClick=“SendMessage”
)绑定的,请尝试替换
intent.getStringExtra(MyActivity.EXTRA_MESSAGE)带有
intent.getStringExtra(“额外消息”)displaymessactivity.java
中的code>也替换
intent.putExtra(额外消息,消息)带有
意图.putExtra(“额外消息”,消息)
@Lal我也很困惑,因为我认为它必须是一个字符串,使得默认值为Hello world!,这就是为什么我的strings.xml中的hello_world是hello,我正在测试它,看看是否发生了这种情况,但它仍然显示hello world!不可能。您没有在代码的任何地方使用该
hello\u world
键。尝试搜索它的使用位置(右键单击“查找用法”)以查看它在第二个屏幕上的显示方式。@Lal该方法是从XML布局声明(
onClick=“SendMessage”
)绑定的,请尝试替换
intent.getStringExtra(MyActivity.EXTRA_MESSAGE)带有
intent.getStringExtra(“额外消息”)displaymessactivity.java
中的code>也替换
intent.putExtra(额外消息,消息)带有
意图.putExtra(“额外消息”,消息)
@Lal我也很困惑,因为我认为它必须是一个字符串,使得默认值为Hello world!,这就是为什么我的strings.xml中的hello_world是hello,我正在测试它,看看是否发生了这种情况,但它仍然显示hello world!非常感谢,拉尔!我真的希望这些差异不是什么大不了的,因为我一直在学习教程。我非常感谢你的帮助!格雷特:)很高兴这有帮助:)<代码>意图.putExtra(“额外消息”,消息)。不能与
intent.getStringExtra(MyActivity.EXTRA_消息)一起使用的。原版还可以。非常感谢,拉尔!我真的希望这些差异不是什么大不了的,因为我一直在学习教程。我非常感谢你的帮助!格雷特:)很高兴这有帮助:)<代码>意图.putExtra(“额外消息”,消息)。不能与
intent.getStringExtra(MyActivity.EXTRA_消息)一起使用的。原版还可以。