2013-12-23 12 views
0

当我制作我的MyFirstApp项目(我按顺序命名为SampleNotes)时,我在CyanogenMod 10.1 Nook HD +上使用内置的ADB通过WiFi运行它,尝试点击发送按钮,它根本就没有做任何事情;就好像我从未添加关于意图或第二项活动的任何内容。谁能帮忙?Android开发人员MyFirstApp发送不起作用

MainActivity.java:

package com.example.samplenotes; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
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.SampleNotes.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) { 
     //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); 
    } 

} 

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.samplenotes" 
    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.samplenotes.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.samplenotes.DisplayMessageActivity" 
      android:label="@string/title_activity_display_message" 
      android:parentActivityName="com.example.SampleNotes.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.SampleNotes.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 

DisplayMessageActivity.java:

package com.example.samplenotes; 

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

public class DisplayMessageActivity extends Activity { 

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

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

     TextView textView = new TextView(this); 
     textView.setTextSize(40); 
     textView.setText(message); 

     setContentView(R.layout.activity_display_message); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 

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

} 

activity_main.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: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=".MainActivity" 
     android:orientation="horizontal" > 

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

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> 

的strings.xml:

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

    <string name="app_name">SampleNotes</string> 
    <string name="action_settings">Settings</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="edit_message">Enter a message</string> 
    <string name="button_send">Send</string> 
    <string name="title_activity_display_message">My Message</string> 

</resources> 

回答

0

你不会真的在发送功能开始活动:

 
//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); 

//add this to start your activity 
startActivity(intent); 

official guide

+0

现在,它会启动活动,但所有的活动确实是打印Hello World,而不是键入到的EditText文本。我怎么能解决这个问题? – George

+0

在第二个活动中,您需要使用通过意图传入的数据来获取它。如果您继续阅读Android指南,您会看到它。 – cbrulak

+0

...我加入了startActivity(intent),甚至在之前;之前的代码给了我一个错误,所以我遵循了这个建议。不幸的是,它像以前一样打印你好世界。 – George

0

在sendMess的最后添加这行年龄功能:

startActivity(intent);

并从第二个活动的onCreate删除行setContentView .. 。

+0

...现在它不说世界你好,它什么也没说。但它转移到第二个活动。 – George

+0

因为你需要setContentView ... – RichieHH

1

正如其他人所说,开始活动,你需要调用:

startActivity(); 

而且,在你的DisplayMessageActivity,你操纵任何控件之前,你必须先设置您的布局:

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

    //first, set main layout 
    setContentView(R.layout.activity_display_message); 

然后让你的文本视图,并设置它:

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

    TextView textView = (TextView) findViewById(R.id.text); 
    textView.setTextSize(40); 
    textView.setText(message); 

而在你activity_display添加ID _message.xml,让您的TextView可以检索:

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