我正在尝试在选择菜单项时启动新的Activity。我已经成功完成了一个菜单项(下载),但现在我正在尝试进行搜索。但是,活动甚至都没有开始。
希望有人可以指出我所缺少的东西。
谢谢!
以下是相关代码:
1)在家长活动中。当我删除意图时,吐司会显示。当我把它重新插入时,应用程序崩溃而没有显示吐司。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_download:
image.setDrawingCacheEnabled(true);
Bitmap bmp = image.getDrawingCache();
String name = random();
if (MediaStore.Images.Media.insertImage(getContentResolver(), bmp, name , "description") != null) {
Context context = getApplicationContext();
CharSequence text = "Downloaded image successfully!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else {
Context context = getApplicationContext();
CharSequence text = "Not successful!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
case R.id.action_search:
Context context = getApplicationContext();
CharSequence text = "Made it to this point!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent j = new Intent(this, GoogleSearchIntent.class);
startActivity(j);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2)在Manifest中:
<activity
android:name="com.example.myfirstapp.GoogleSearchIntent"
android:label="@string/search_text" >
</activity>
编辑:我已按建议添加了break语句,但仍然没有运气。
另一个编辑:我改变了意图以切换到我的其他活动之一。这工作很精细,那么我的活动GoogleSearchIntent会导致问题吗?
public class GoogleSearchIntent extends Activity {
private EditText editTextInput;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remain);
// editTextInput = (EditText) findViewById(R.id.editTextInput);
}
public void onSearchClick(View v)
{
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
感谢您的帮助!!
答案 0 :(得分:0)
你忘了&#34;打破&#34;
case R.id.action_download:
image.setDrawingCacheEnabled(true);
Bitmap bmp = image.getDrawingCache();
String name = random();
if (MediaStore.Images.Media.insertImage(getContentResolver(), bmp, name , "description") != null) {
Context context = getApplicationContext();
CharSequence text = "Downloaded image successfully!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
}
else {
Context context = getApplicationContext();
CharSequence text = "Not successful!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
}
break;
case R.id.action_search:
Context context = getApplicationContext();
CharSequence text = "Made it to this point!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
Intent j = new Intent(this, GoogleSearchIntent.class);
startActivity(j);
break;
default:
return super.onOptionsItemSelected(item);
}
你应该用这种方式初始化祝酒词:
Toast toast = Toast.makeText(this, text, duration);
或者只需一步调用它们:
Toast.makeText(this, R.string.prompt_to_check_gps, Toast.LENGTH_LONG).show();