2014-04-14 67 views
1

我在加載另一個activity.I後跟Tutorial當我點擊按鈕「發送」,應用程序停止。任何人都能確定我的錯誤?Android的第一個應用程序錯誤(開始另一個活動)

這裏是我的MainActivity.java

package com.example.myfirstapp; 

    import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.support.v7.app.ActionBarActivity; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.ViewGroup; 
    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); 

      if (savedInstanceState == null) { 
       getSupportFragmentManager().beginTransaction() 
         .add(R.id.container, new PlaceholderFragment()).commit(); 
      } 
     } 

     @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) { 
      Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class); 
      EditText editText = (EditText) findViewById(R.id.edit_message); 
      String message = editText.getText().toString(); 
      intent.putExtra(EXTRA_MESSAGE, message); 
      startActivity(intent); 
     } 

/** 
* 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.fragment_main, container,false); 
       return rootView; 
      } 
     } 

    } 

這裏是我的DisplayMeassageActivity.java:

public class DisplayMessageActivity extends ActionBarActivity { 


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


    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 
    setContentView(textView); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.display_message, 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); 
} 

/** 
* 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.fragment_display_message, 
       container, false); 
     return rootView; 
    } 
} 

}

這裏是我的fragment_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:orientation="horizontal" 
      tools:context="com.example.myfirstapp.MainActivity$PlaceholderFragment" > 
     <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> 

一第二AndroidManifest.xml中

<?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="8" 
      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.example.myfirstapp.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.myfirstapp.DisplayMessageActivity" 
        android:label="@string/title_activity_display_message" 
        android:parentActivityName="com.example.myfirstapp.MainActivity" > 
       <meta-data 
        android:name="android.support.PARENT_ACTIVITY" 
        android:value="com.example.myfirstapp.MainActivity" /> 
      </activity> 

     </application> 

    </manifest> 
+2

發佈日誌... – Noob

+0

是啊需要你的日誌。如果你不知道你的日誌在哪裏,在eclipse中單擊窗口 - > show view - > other - > android - > logcat。運行您的應用程序,然後向我們顯示日誌錯誤(以及與錯誤相關的行,如果您知道如何找到它) – zgc7009

回答

0

問題是與你DisplayMessageActivityonCreate

您正試圖在contentView設置之前添加Fragment

Aslo你的setContentView(textView)只設置了TextView

所以,你需要刪除代碼添加一個片段或創建一個佈局,並設置ContentView找到View之前。

+0

謝謝,它有幫助! – Ridon

相關問題