Android 如何在eclipse中解决此致命异常?
这是我的应用程序的代码,我正在使用Eclipse DisplayMessageActivity.javaAndroid 如何在eclipse中解决此致命异常?,android,android-2.2-froyo,Android,Android 2.2 Froyo,这是我的应用程序的代码,我正在使用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
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);
}
}
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);
}
}
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>
如果您需要更多信息或调试结果,请告诉我。我正在学习Android开发者教程。如果没有LogCat,我可以解决您代码中的一个问题<代码>@suppressint(“NewApi”)。Lint是一个很好的观点——您的目标API是8。弗罗约和姜饼没有动作条 现在,在
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();
这样,Android会检查API版本,如果低于蜂巢的API版本,则不会调用姜饼上不存在的
getActionBar()
方法。这么多代码,但没有LogCat堆栈跟踪。没有LogCat是什么意思?每当应用程序崩溃时,如果启用USB调试,eclipse中的LogCat视图将输出红线。这就是堆栈跟踪,这有助于您找出错误所在。另外,您的手机/仿真器是什么API@suppressint(“NewApi”)
如果您使用的是比蜂巢更旧的手机,可能是一个原因。我的手机是Galaxy型号,运行Gingerbread.FF19型号是SPH-M820-BST如果我想针对大多数Android设备,我应该将“NewApi”改成什么?谢谢!我再也不会撞车了。