动作条不工作(ActionBar not working)

2019-10-19 03:24发布

我需要相关操作栏的工作提供一些指导。 其实点击操作栏上的MenuItem 。 当前活动必须转移到另一个活动。 但它什么都不做。 甚至没有任何错误。 我用下面的代码:

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

Answer 1:

使用MenuInflater inflater = getSupportMenuInflater();

如果你使用的是Android主按钮,然后id为android.R.id.home 。 在这种情况下,请确保您有getSupportActionBar().setDisplayHomeAsUpEnabled(true); 在你onCreate

编辑 - 请确保您已导入这些:

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


Answer 2:

把onCreate方法这里面的代码:

@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();
    ...

}

然后把外面的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); 
}


Answer 3:

这个代码是完美的。 没有这个代码的任何问题。 动作条按钮是不是因为工作,空值是receiving..and空值只是因为一些硬件issues..nothing错这个代码谢谢



文章来源: ActionBar not working