gpt4 book ai didi

android - toast 不显示

转载 作者:行者123 更新时间:2023-11-29 14:19:46 27 4
gpt4 key购买 nike

我想在里面的 actionItem() 函数上显示 Toast。但它现在不显示 Toast。

如果我打电话

Context context = getApplicationContext();

在 actionItem() 之前,应用会崩溃。

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class ShowWebViewActivity extends ActionBarActivity {

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

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_show_web_view, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();

//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }

switch (item.getItemId()) {
case R.id.action_settings:
actionItem();
return true;
default:
return super.onOptionsItemSelected(item);
}

}
// Context context = getApplicationContext() makes app to crash why??
public void actionItem(){
// Toast function should call here. But not working
Context context = getApplicationContext();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
}


}

最佳答案

应该是,

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

关于android - toast 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30567497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com