简体   繁体   中英

Android app crashes when clicked on the actionbar/titlebar

I am developing an android application (for jellybean) which has 9 activities connected with intents. 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:

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. in adt22 the location of android-support-v4.jar is changed which caused this error. the solution is here: Libraries do not get added to APK anymore after upgrade to ADT 22

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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