Question

I have two activities with two layouts and a action bar. The minimum API I am using is 14. I go from one activity to another with no problem using intent, and I want to make a home button in the action bar to return to the activity that I choose. I implemented a menu and called it from xml using the guides on the android developers site, but when I click on the button/menu from the action bar, it crashes with uncaught exception.

The main activity class:

         package com.example.quiz;

         import android.app.ActionBar;
         import android.app.Activity;
         import android.content.Intent;
         import android.os.Bundle;
         import android.view.Menu;
         import android.view.MenuInflater;
         import android.view.MenuItem;
         import android.view.View;
         import android.view.View.OnClickListener;
         import android.widget.Button;

         public class MainActivity extends Activity {

     Button new_b;
     ActionBar actionBar;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.show();


         new_b=(Button)findViewById(R.id.new_button);

         new_b.setOnClickListener(new OnClickListener() {
         @Override   
         public void onClick(View v) { 

            Intent intent = new Intent(MainActivity.this,HomeActivity.class);
            startActivity(intent);
        };
          });   

         }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);
        return super.onCreateOptionsMenu(menu);
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.home_button:homeClick();
                return true;
            case R.id.info_button:homeClick();
                return true;
            default:
                return super.onOptionsItemSelected(item);                   
        }           
     }

     private void homeClick() {
        Intent intent_home = new Intent(this,HomeActivity.class);
        intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent_home);     
     }
        }
       }

The home activity class:

         import android.app.ActionBar;
         import android.app.Activity;
         import android.content.Intent;
         import android.os.Bundle;
         import android.view.Menu;
         import android.view.MenuInflater;
         import android.view.MenuItem;

         public class HomeActivity extends Activity {   

     ActionBar actionBar;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    ActionBar actionBar = getActionBar();
    actionBar.show();

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_actions, menu);
        return super.onCreateOptionsMenu(menu);
     }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.home_button:homeClick();
                return true;
            case R.id.info_button:homeClick();
                return true;
            default:
                return super.onOptionsItemSelected(item);                
        }

     }

     private void homeClick() {
        Intent intent_home = new Intent(this,HomeActivity.class);
        intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent_home);    
      }
         }
        }

Can I make it another way, or what can I do to make it work?

           04-20 19:31:57.610: I/Adreno200-EGL(5708): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build:
           AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)
           04-20 19:31:57.610: I/Adreno200-EGL(5708): Build Date: 02/25/13 Mon
           04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Branch: 
           04-20 19:31:57.610: I/Adreno200-EGL(5708): Remote Branch: quic/jb_rel_2.0.3
           04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Patches: NONE
           04-20 19:31:57.610: I/Adreno200-EGL(5708): Reconstruct Branch AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 +  NOTHING
           04-20 19:31:57.850: W/ResourceType(5708): getEntry failing because entryIndex 14 is beyond type entryCount 1
           04-20 19:31:57.850: W/ResourceType(5708): Failure getting entry for 0x7f0b000e (t=10 e=14) in package 0 (error -2147483647)
           04-20 19:32:09.390: E/dalvikvm(5708): GC_CONCURRENT freed 2965K, 22% free 12424K/15751K, paused 12ms+26ms, total 75ms
           04-20 19:32:10.600: W/dalvikvm(5708): threadid=1: thread exiting with uncaught exception (group=0x41d02438)
Was it helpful?

Solution 2

It turned out that I was trying to format my strings with html code.

Example:

I was using:

    <string name="title"> <b>App title</b> </string>

When I was supposed to use:

    <string name="title"> App title <string>

OTHER TIPS

You can use this code as an example:

MainActivity.java

package com.example.quiz;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.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_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.go_home:
                Intent intentHome = new Intent(this, HomeActivity.class);
                startActivity(intentHome);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

/res/menu/activity_main_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/go_home"
        android:showAsAction="ifRoom"
        android:title="@string/go_home" />

</menu>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top