简体   繁体   English

单击操作栏/标题栏时,Android应用程序崩溃

[英]Android app crashes when clicked on the actionbar/titlebar

I am developing an android application (for jellybean) which has 9 activities connected with intents. 我正在开发一个Android应用程序(用于软糖),其中有9个与意图相关的活动。 The application crashes when I click the bar at the top (which is supposed to take me to the previous activity I guess). 当我单击顶部的栏时,该应用程序崩溃(应该将我带到我猜之前的活动)。 I'm not sure what that bar is called but it shows the name of the current activity. 我不确定该栏的名称,但是它显示了当前活动的名称。 It does not crash when I click the hardware "back" button but when I click that bar it says "the app has stopped". 当我单击硬件“后退”按钮时,它不会崩溃,但是当我单击该栏时,它显示“应用程序已停止”。 I'm adding the screenshot of the emulator to show which bar I'm talking about: 我正在添加模拟器的屏幕截图,以显示我正在谈论的栏: 屏幕截图

here is my main activity code and the profile activity code: 这是我的主要活动代码和个人资料活动代码:

MAIN ACTIVITY: 主要活动:

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

}

PROFILE ACTIVITY(the one in the screenshot): 配置文件活动(屏幕截图中的一个):

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

}

Can someone please explain why that is happening? 有人可以解释为什么会这样吗? If not how can I remove this bar from my activities? 如果没有,如何从活动中删除该栏?

EDIT: logcat is below: 编辑: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)

I found the solution. 我找到了解决方案。 The problem was upgrading to adt22. 问题是升级到adt22。 in adt22 the location of android-support-v4.jar is changed which caused this error. 在adt22中,更改了android-support-v4.jar的位置,这导致了此错误。 the solution is here: Libraries do not get added to APK anymore after upgrade to ADT 22 解决方案在这里: 升级到ADT 22后,不再将库添加到APK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM