我有两个活动,包括两个布局和一个操作栏。 我使用的最小API是14。 我使用intent从一个活动转到另一个活动没有问题,我想在操作栏中创建一个主页按钮以返回我选择的活动。 我实现了一个菜单,并使用Android开发者网站上的指南从xml调用它,但当我点击操作栏中的按钮/菜单时,它会因未捕获的异常而崩溃。
主要活动类:
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);
}
}
}
家庭活动课程:
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);
}
}
}
我可以采用另一种方式,或者我可以做些什么来使其发挥作用?
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)
答案 0 :(得分:0)
您可以使用此代码作为示例:
<强> 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>
答案 1 :(得分:0)
原来我试图用html代码格式化我的字符串。
示例:
我在使用:
<string name="title"> <b>App title</b> </string>
当我应该使用时:
<string name="title"> App title <string>