2014-09-04 102 views
0

我下面从以下链接教程:https://developer.android.com/training/basics/firstapp/starting-activity.html无法从父活动接收意向?

我面临的问题是,我能够触发但是sendMessage方法,它只是没有表现出预期的效果?

请看看下面的代码:

activity_main.xml中(父布局):

<EditText android:id="@+id/edit_message" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     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> 

MainActivity.java:

package com.example.myfirstapp; 

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


public class MainActivity extends ActionBarActivity { 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.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; 
    } 

    @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(); 
     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) { 
     // Do something in response to button 
     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); 
    } 
} 

activity_display_message .xml(儿童la YOUT):

<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.myfirstapp.DisplayMessageActivity" > 

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

</RelativeLayout> 

DisplayMessageActivity.java:

package com.example.myfirstapp; 

import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class DisplayMessageActivity extends ActionBarActivity { 

    @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) { 
     // 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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.activity_display_message, 
         container, false); 
       return rootView; 
     } 
    } 
} 

MyFirstApp清单:

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

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

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

</manifest> 

P/S:我在DisplayMessageActivity.java onCreate()设置一个断点方法,但它不是贝ng触发。

+0

实际的问题是什么?显示的结果是什么? – 2014-09-04 08:28:03

+0

尝试更改您的问题 – pskink 2014-09-04 08:28:08

+0

@SamDunk:点击发送按钮后没有任何回应 – 2014-09-04 08:28:51

回答

2

您缺少一条startActivity声明。你MainActivity.sendMessage它应该像follwing:

/** Called when the user clicks the Send button */ 
public void sendMessage(View view) { 
    // Do something in response to button 
    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); /// <---- THIS LINE 
} 

希望它可以帮助

0

添加以下行

startActivity(intent); 

intent.putExtra(EXTRA_MESSAGE, message); 

和呼叫的sendMessage()按钮点击

0

1)您没有将Listener附加到您的按钮上。不喜欢它:

Button myButton = (Button) findViewById(R.id.my_button); 
myButton.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 
       sendMessage(); //this is the action to be taken. 
      } 
     }); 

2)您所创建的Intent对象,但没有触发它。使用startActivity(intent)作为最后一行sendMessage()方法来激发意图并获得期望的结果。

0

此行

intent.putExtra(EXTRA_MESSAGE, message); 

这为什么有在点击按钮无反应后添加

startActivity(intent)