2012-12-24 37 views
-1

這是我的應用程序的編碼,我正在使用Eclipse。如何在eclipse中解決這個致命的異常?

DisplayMessageActivity.java

package com.example.myfirstapp; 

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

@SuppressLint("NewApi") public class DisplayMessageActivity extends Activity 
{ 
    @SuppressLint("NewApi") @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 
     // Show the Up button in the action bar. 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     //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 
     setContentView(textView); 
    } 

// OKAY TO REMOVE 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_display_message, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
     case android.R.id.home: 
      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 


MyFirstAppManifest.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="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" android:debuggable="true"> 
     <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> 


MainActivity.java

package com.example.myfirstapp; 

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.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.activity_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); 
     startActivity(intent); 
    } 
} 

如果您需要更多信息或調試結果,請讓我知道。我正在關注位於here的Android開發者教程。

+6

這麼多的代碼,但沒有LogCat堆棧跟蹤。 –

+0

沒有LogCat是什麼意思? –

+1

每當應用程序崩潰時,如果啓用USB調試,則eclipse中的LogCat視圖將輸出紅色線條。這是堆棧跟蹤,這可以幫助您找出錯誤的位置。另外,你的手機/模擬器是什麼API? '@SuppressLint(「NewApi」)'可能是一個原因,如果你使用的是比蜂窩更早的東西。 –

回答

1

沒有LogCat,我可以解決您的代碼中的一個問題。 @SuppressLint("NewApi")。 Lint提供了一個很好的觀點 - 你的目標API是8.Froyo和薑餅沒有一個Action Bar。

現在,在DisplayMessageActivity做出這樣的方法:

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // Show the Up button in the action bar. 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 

現在走這條線出你的代碼DisplayMessageActivity

getActionBar().setDisplayHomeAsUpEnabled(true); 

setupActionBar(); 

代替它那way Android會檢查API版本,如果它在下面,HoneyComb's不會調用getActionBar()薑餅上不存在的方法。

+0

謝謝!我不再發生崩潰。 –

相關問題