开始另一项活动 - 活动未显示我的消息

时间:2015-08-16 16:36:38

标签: java android

我是android新手,但有python和java的经验,所以我决定要创建应用程序,因此我从developer.android.com开始教程。

我正在构建您的第一个应用>启动另一个活动部分。

我没有错误地进入最后一步,但是当我按下发送按钮时,我输入的信息没有显示在第二个活动中!

https://developer.android.com/training/basics/firstapp/starting-activity.html#DisplayMessage)如果您单击该链接以查看教程,并向下滚动到底部,您应该会看到您键入的消息应该弹出,并且字体大小会更大。 我在输入ANYTHING时找到的消息是小字体,默认为“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);
}

}

这是DisplayMessageActivity.java

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_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>

下面的activity_my.xml:

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

下面的strings.xml:

<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!”当我按下发送时弹出新活动。

感谢您抽出宝贵时间,对不起,感谢对不起!

1 个答案:

答案 0 :(得分:0)

首先,替换

intent.putExtra(EXTRA_MESSAGE, message);

intent.putExtra("EXTRA_MESSAGE", message);

MyActivity.java

因为putExtra()的第一个参数应该是一个String名称,它在应用程序中进一步使用。请参考[docs](http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String,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"/>