2013-11-03 52 views
0

我是新來學習Android開發,我下面this tutorial,使一個小的應用程序。我只是改了一些代碼。當我點擊發送按鈕時,我得到一個提示,說:「不幸的是,我的第一個應用程序已停止。」有人可以告訴我什麼事情。爲什麼我的簡單的應用程序停止

我取代教程(這裏示出爲在第二類的代碼塊註釋)中給出的代碼,並通過其在此處的代碼替換爲含有*意見使突出。

第一項活動: -

package com.practice.firstapp1; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import com.practice.firstapp1.R; 

public class MainActivity extends Activity { 

    public final static String EXTRA_MESSAGE= "com.practice.fristapp1.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 */ 
    /** 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.id1); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 

} 

佈局第一項活動: -

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

2ND活動代碼: -

package com.practice.firstapp1; 

    import android.annotation.SuppressLint; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Build; 
    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 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_display_message); 

      // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       // Show the Up button in the action bar. 
       getActionBar().setDisplayHomeAsUpEnabled(true); 
      } 

      //Get the intent which invoked this activity and read its extra's data into the string named message. 
      Intent intent = getIntent(); 
      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

      TextView textView = (TextView) findViewById(R.id.text_view_2); //************ 
      textView.setText(message);//*********** 

      setContentView(textView);//*********** 
//The above three statements along with statements from its layout (which have been given *s in comments have been placed instead of the statements in the following block comment. Because I wanted to create the TextView in layout.xml and handle its work in code. 

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

2ND活性的佈局: -

<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:id="@+id/text_view_2" <!--//***********************--> 
     android:layout_width="wrap_content" <!--//**********************--> 
     android:layout_height="wrap_content" <!--//**************--> 
     android:textSize="40sp" /> <!--//**************************--> 


</RelativeLayout> 

Android清單文件: -

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

</manifest> 
+1

您可以張貼在logcat的錯誤嗎? –

+0

你有清單文件中的活動嗎?發佈清單文件 – Raghunandan

+0

,你並不需要這種'的setContentView(TextView的);' – Raghunandan

回答

1

從您的意見

I havn't done anything in the manifest file. I think it is done automatically when we are using eclipse. 

是日食確實增加了活動,如果你創建一個新的Android活動來體現。

你的表現看起來不錯。刪除下面的行

編輯1:

setContentView(textView); 

activity_display_message.xml已經有一個TextView,並在你的代碼初始化你的TextView和設置的文本。 TextView已經有一個父類。

無需setContentView(textView)

+0

清單已包含這兩個活動。 – user2882662

+0

@ user2882662 ok發佈logcat。在Eclipse的goto窗口 - >打開透視 - >打開其他 - >選擇ddms點擊logcat你應該看到一個異常發佈在這裏和沒有害處是張貼清單文件只是爲了確認 – Raghunandan

+0

請看我下面的兩個答案日誌貓和清單。我不知道如何在評論中添加圖片。 – user2882662

相關問題