无法开始另一项活动

时间:2014-01-30 21:58:04

标签: android-activity android

我多次按照http://developer.android.com/training/basics/firstapp/creating-project.html上的完整说明操作,无法开始活动。该应用程序基本上有一个文本字段和发送按钮。当我在文本字段中输入文本并点击发送按钮时,应该启动另一个活动。但是,使用我当前的代码,点击发送按钮什么都不做。


MySecondApp / SRC / MainActivity.java


package com.example.mysecondapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";

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

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

    /** Called when the user clicks the Send button */
    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);
    }

}

MySecondApp / SRC / DisplayMessageActivity.java


package com.example.mysecondapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @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);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MySecondApp / RES /布局/ activity_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>

MySecondApp / RES /布局/ activity_display_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=".DisplayMessageActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

MySecondApp / RES /值/ strings.xml中


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Safe\'s 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">My Message</string>
    <string name="hello_world">Hello world!</string>

</resources>

MySecondApp / AndroidManifest.xml中


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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mysecondapp.MainActivity"
            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="com.example.mysecondapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.mysecondapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.mysecondapp.MainActivity" />
        </activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:1)

您的问题出在第一个活动的xml中。 Button中有两个终止字符。删除/&gt;在你的android:文本行之后。

答案 1 :(得分:1)

改变你的xml:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" />
            android:onClick="sendMessage" />

为:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" 
            android:onClick="sendMessage" />

而且,我认为你应该在onCreate方法中引用你的按钮。

您还可以尝试将代码更改为:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";

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

        Button yourButton = (Button) findViewById(R.id.yourid);

        yourButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Main.this, YourSecondActivity.class);
                EditText editText = (EditText) findViewById(R.id.yourEditText);
                String message = editText.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);

            }
        });

    }

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

}

并从以下位置更改您的xml:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" />
            android:onClick="sendMessage" />

要:

 <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_send" 
                />

答案 2 :(得分:0)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />
    android:onClick="sendMessage" />

看看最后一行是如何变黑的?那是因为您使用/>

结束了按钮标记两次

所以在第二行最后一行取消结束标记:android:text="@string/button_send"

您的代码将是:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />

答案 3 :(得分:0)

当找到onClick属性并从下拉列表中选择sendMessage [MainActivity]时,在.xml文件中:android:onClick =“sendMessage(MainActivity)”。 删除“(MainActivity)”: 所以,在.xml文件:android:onClick =“sendMessage” 然后再次运行应用程序!