ActionBar not working

房东的猫 提交于 2019-12-11 12:34:34

问题


I need some guidance regarding the working of Action bar. Actually clicking on a Action bar MenuItem. Current activity must be moved to another activity. But it's doing nothing. Not even any error. I used following code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mnew1, 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:
            try {
                openSearch();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

private void openSearch() throws IOException{
    val=address(location.getLatitude(), location.getLongitude());
    Intent intnt=new Intent(getApplicationContext(),SendSms.class);
    intnt.putExtra("loct", val);
    startActivity(intnt); 
}

回答1:


Use MenuInflater inflater = getSupportMenuInflater();

And if you are using android home button then the id is android.R.id.home. In that case make sure you have getSupportActionBar().setDisplayHomeAsUpEnabled(true); in your onCreate.

EDIT- Make sure you have imported these:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;



回答2:


put this code inside onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_main_activity_xml);

    // creating Actionbar
    ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.show();
    ...

}

and then put this code outside onCreate :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mnew1, 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:
        try {
            openSearch();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

private void openSearch() throws IOException{
val=address(location.getLatitude(), location.getLongitude());
Intent intnt=new Intent(getApplicationContext(),SendSms.class);
intnt.putExtra("loct", val);
startActivity(intnt); 
}



回答3:


this code is perfect. There is not any problem with this code. Actionbar button was not working due to null value it is receiving..and null value was just because of some hardware issues..nothing wrong with this code thanks



来源:https://stackoverflow.com/questions/20604406/actionbar-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!