2014-03-19 45 views
0

我是Android新手,並且我正在關注用於學習android的this tutorial。該應用程序接受來自用戶的輸入,並在用戶按下按鈕Submit時打印文本。它可以從用戶那裏獲得輸入,但是當按下Submit按鈕時它不會輸出任何東西。 Eclipse不會在代碼中顯示任何錯誤,所以我不知道什麼是錯的。下面是代碼(對不起,粘貼這麼多的代碼,因爲我無法做出來的文件正是導致錯誤):當在Android應用程序中點擊發送按鈕時沒有收到任何迴應

的HelloWorld/src目錄/ com.archit.helloworld/DisplayMessageActivity.java

package com.archit.helloworld; 

import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.Fragment; 
import android.content.Intent; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import com.archit.helloworld.R; 

public class DisplayMessageActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 
     // Show the Up button in the action bar. 
     setupActionBar(); 
     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

     //Create new text view 
     TextView textView = new TextView(this); 
     textView.setTextSize(40); 
     textView.setText(message); 

     //set the text view as the activity layout 
     setContentView(textView); 
    } 

    /** 
    * Set up the {@link android.app.ActionBar}, if the API is available. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      getActionBar().setDisplayHomeAsUpEnabled(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); 
    } 

    /** 
    * 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; 
     } 
    } 
} 

的HelloWorld/SRC/com.archit.helloworld/MainActivity.java

package com.archit.helloworld; 

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.helloworld.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); 
    } 

} 

的HelloWorld/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> 

的HelloWorld/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> 

的HelloWorld/AndroidManifest.xml中

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

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

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

</manifest> 
+0

使用startActivity(意向);在你指定的意圖之後。 – Fabian

回答

2
/** 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); 
} 

必須

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

} 
+0

謝謝!這是問題。我沒有仔細看過[this](http://developer.android.com/training/basics/firstapp/starting-activity.html)。它寫在那裏。 –

1

實施onclicklistener哪裏是你onclickListener

+0

不是neccesairy,檢查他的xml – Fabian

+0

ohhkkk對不起,請仔細閱讀 – rup35h

1

其實你忘了添加startActivity方法

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); 
    } 
1

你已經錯過來電

startActivity(intent); 

sendMessage(View V) 

方法。所以,最終這將是

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

}

相關問題