我正在开发一个 android 应用程序(用于 jellybean),它有 9 个与意图相关的活动。当我单击顶部的栏时应用程序崩溃(我猜这应该带我进入上一个活动)。我不确定那个栏叫什么,但它显示了当前活动的名称。当我单击硬件“返回”按钮时它不会崩溃,但是当我单击该栏时它会显示“应用程序已停止”。我正在添加模拟器的屏幕截图以显示我正在谈论的栏:
这是我的主要活动代码和个人资料活动代码:
主要活动:
package com.ecotravel.eco_travel;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText usernamefield= (EditText) findViewById(R.id.usernamefield);
EditText passwordfield = (EditText) findViewById(R.id.passwordfield);
}
@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 Log In button */
public void loginFunc(View view) {
Intent intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
EditText usernamefield= (EditText) findViewById(R.id.usernamefield);
EditText passwordfield = (EditText) findViewById(R.id.passwordfield);
}
/** Called when the user clicks the Register button */
public void registerFunc(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
}
个人资料活动(屏幕截图中的那个):
package com.ecotravel.eco_travel;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class ProfileActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* 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 onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile, 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);
}
/** Called when the user clicks the Search button */
public void searchFunc(View view) {
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
}
/** Called when the user clicks the Wishlist button */
public void wishFunc(View view) {
Intent intent = new Intent(this, WishlistActivity.class);
startActivity(intent);
}
}
有人可以解释为什么会这样吗?如果不是,我如何从我的活动中删除此栏?
编辑:logcat 如下:
06-08 17:50:12.140: E/AndroidRuntime(3235): FATAL EXCEPTION: main
06-08 17:50:12.140: E/AndroidRuntime(3235): java.lang.NoClassDefFoundError: android.support.v4.app.NavUtils
06-08 17:50:12.140: E/AndroidRuntime(3235): at com.ecotravel.eco_travel.ProfileActivity.onOptionsItemSelected(ProfileActivity.java:51)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.app.Activity.onMenuItemSelected(Activity.java:2548)
06-08 17:50:12.140: E/AndroidRuntime(3235): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.view.View.performClick(View.java:4204)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.view.View$PerformClick.run(View.java:17355)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.os.Handler.handleCallback(Handler.java:725)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.os.Looper.loop(Looper.java:137)
06-08 17:50:12.140: E/AndroidRuntime(3235): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-08 17:50:12.140: E/AndroidRuntime(3235): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 17:50:12.140: E/AndroidRuntime(3235): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 17:50:12.140: E/AndroidRuntime(3235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-08 17:50:12.140: E/AndroidRuntime(3235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-08 17:50:12.140: E/AndroidRuntime(3235): at dalvik.system.NativeStart.main(Native Method)