本文介绍了打开定制对话框时强制关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了按键的自定义对话框,但是当我去启动它,我的应用程序强制关闭。我有对话框而不自定义按钮前面的工作。而且我不知道我在哪里搞乱了这里:
公共类BeerPage扩展ActionbarMenu { BeerDataê;
对话的对话= NULL;
AlertDialog.Builder建设者; 保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.beer_page);
建设者=新AlertDialog.Builder(本); //从列表视图中的数据
意向意图= getIntent();
束B = intent.getExtras();
E = b.getParcelable(myBeerObject);
//测试共享preFS
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
字符串username = prefs.getString(username的,NULL);
字符串userid = prefs.getString(用户ID,NULL); //设置图像
ImageView的IM1 =(ImageView的)findViewById(R.id.image);
如果(e.largeLabel.equals(N / A)){
//设置图像PNG
im1.setImageResource(R.drawable.noimage);
} 其他{
ImageDownloadTask成像=新ImageDownloadTask(IM1);
imageD.execute(e.largeLabel);
} //检查用户是否有啤酒
字符串URL =myURL;
字符串userURLComp =U =+用户ID;
串beerID =和b =+ e.beerId; URL =网址+ userURLComp + beerID;
新CheckBeerJSON(这一点,e.beerId).execute(URL);
字符串URL2 =myURL2;
新GetBeerRateJSON(这一点,e.beerId).execute(URL2); //膨胀形象洛德
LayoutInflater mInflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
的LinearLayout Add按钮=(的LinearLayout)((活动)本).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.checkingportfolio,NULL)); // prepare按钮
//按钮buttonBrewery =(按钮)findViewById(R.id.buttonBrewery);
//按钮按钮样式=(按钮)findViewById(R.id.buttonStyle); // prepare文字的东西
TextView的TV1 =(的TextView)findViewById(R.id.beerTitle);
TextView中TV2 =(的TextView)findViewById(R.id.beerDescription);
TextView的tv_ibu =(的TextView)findViewById(R.id.IBU);
TextView的tv_abv =(的TextView)findViewById(R.id.abv);
TextView的tv_glass =(的TextView)findViewById(R.id.glass);
TextView的tv_breweryName =(的TextView)findViewById(R.id.beerBreweryName);
TextView的tv_styleName =(的TextView)findViewById(R.id.beerStyleName); //设置文本thinsg
tv1.setText(e.beerName);
tv2.setText(e.beerDescription); 字符串breweryButton =啤酒;
字符串styleButton =风格:; tv_breweryName.setText(e.beerBreweryName);
tv_styleName.setText(e.beerStyle);
breweryButton = breweryButton + e.beerBreweryName;
styleButton = styleButton + e.beerStyle;
//buttonBrewery.setText(breweryButton);
//buttonStyle.setText(styleButton);
tv_ibu.setText(e.beerIBU);
tv_abv.setText(e.beerABV);
tv_glass.setText(e.beerGlass); //Toast.makeText(this,e.mediumLabel,Toast.LENGTH_SHORT).show(); } @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main2,菜单); 返回true;
} //观点啤酒厂功能
公共无效viewBrewery(查看视图){ //推出新啤酒厂页面类
意图I =新意图(这一点,BreweryPage.class);
i.putExtra(myBeerObject,E);
i.setClass(这一点,BreweryPage.class);
startActivity(ⅰ);
}公共无效viewStyle(查看视图){ //推出新啤酒厂页面类
意图I =新意图(这一点,BreweryPage.class);
i.putExtra(myBeerObject,E);
i.setClass(这一点,StylePage.class);
startActivity(ⅰ);
}//观点啤酒厂功能
公共无效viewTasteTags(查看视图){ //推出新啤酒厂页面类
意图I =新意图(这一点,BreweryPage.class);
i.putExtra(myBeerObject,E);
i.setClass(这一点,TasteTags.class);
startActivity(ⅰ);
}公共字符串连接codeThisWord(字符串字){ 尝试{
字= URLEn coder.en code(文字,UTF-8);
}赶上(UnsupportedEncodingException五){
// TODO自动生成catch块
e.printStackTrace();
} 返回字;}公共无效addBeer(查看视图){ //获取用户信息
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
字符串username = prefs.getString(username的,NULL);
字符串userid = prefs.getString(用户ID,NULL);
//获取啤酒细节
字符串URL =http://www.beerportfolio.com/app_addBeer.php?
字符串urlUserID =U =+用户ID;
字符串urlBeerID =&放大器;出价=+ e.beerId;
字符串urlBeerName =&放大器; BNAME =EN + codeThisWord(e.beerName);
//构造URL添加啤酒
URL =网址+ urlUserID + urlBeerID + urlBeerName; Log.d(URL,URL); //执行上的网址异步添加到啤酒厂
新AddBeer(本).execute(URL); //做:变化开始评价者
LL的LinearLayout =(的LinearLayout)findViewById(R.id.addBeerLayout);
ll.removeAllViews(); //添加评价者 LayoutInflater吹气=(LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
的LinearLayout Add按钮=(的LinearLayout)this.findViewById(R.id.addBeerLayout);
addButton.addView(inflater.inflate(R.layout.addrate_layout,NULL)); addListenerOnRatingBar(本);}私人无效addListenerOnRatingBar(BeerPage beerPage){
的RatingBar的RatingBar =(的RatingBar)this.findViewById(R.id.beerRatingBar); ratingBar.setOnRatingBarChangeListener(新OnRatingBarChangeListener(){
公共无效onRatingChanged(的RatingBar的RatingBar,浮动额定值,
布尔FROMUSER){ //接下来的异步任务来更新在线数据库
浮星= ratingBar.getRating(); //获取用户详细信息
共享preferences preFS = preferenceManager.getDefaultShared preferences(BeerPage.this);
字符串userid = prefs.getString(用户ID,NULL); //获取ID啤酒
字符串beerID = e.beerId; //获取评级
串urlRate =R =+将String.valueOf(ratingBar.getRating());
字符串urlUserID =&放大器; U =+用户ID;
串urlBeerID =和b =+ beerID; //构造URL
字符串URL2 =http://www.beerportfolio.com/app_rateUpdate.php? URL2 = URL2 + urlRate + urlUserID + urlBeerID; Log.d(addRateingURL2); //异步任务来更新评级数据库
新UpdateRating(BeerPage.this).execute(URL2); }
});} 公共无效rateDialog(视图v){
//定制对话框 AlertDialog.Builder建设者=新AlertDialog.Builder(本);
//添加按钮
builder.setPositiveButton(拯救,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//用户点击OK按钮
}
});
builder.setNegativeButton(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//用户取消了对话框
}
});
//设置其他的属性对话框
//创建AlertDialog
AlertDialog对话框= builder.create(); dialog.setContentView(R.layout.rate_stars);
dialog.setTitle(评估这啤酒);
dialog.show();
}}
我得到的部队密切错误是:
20 09-26:37:43.479 29047-29047 / com.beerportfolio.beerportfoliopro E / AndroidRuntime:致命异常:主要
java.lang.IllegalStateException:无法执行活动的方法
在android.view.View $ 1.onClick(View.java:3609)
在android.view.View.performClick(View.java:4102)
在android.view.View $ PerformClick.run(View.java:17126)
在android.os.Handler.handleCallback(Handler.java:615)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:155)
在android.app.ActivityThread.main(ActivityThread.java:5536)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1074)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
在dalvik.system.NativeStart.main(本机方法)
java.lang.reflect.InvocationTargetException:产生的原因
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在android.view.View $ 1.onClick(View.java:3604)
... 11更多
android.util.AndroidRuntimeException:引起requestFeature()必须添加内容之前调用
在com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:322)
在com.android.internal.app.AlertController.installContent(AlertController.java:242)
在android.app.AlertDialog.onCreate(AlertDialog.java:336)
在android.app.Dialog.dispatchOnCreate(Dialog.java:364)
在android.app.Dialog.show(Dialog.java:261)
在com.example.beerportfoliopro.BeerPage.rateDialog(BeerPage.java:326)
... 14个
解决方案
@ codeMagic接近,但完整的答案, cribbed,就是要做到这一点:
//创建AlertDialog
builder.setTitle(评估这啤酒);
builder.setView(getLayoutInflater()膨胀(R.layout.rate_stars,NULL));
AlertDialog对话框= builder.create();//删除dialog.setContentView(R.layout.rate_stars);
//删除dialog.setTitle(评估这啤酒);
dialog.show();
希望这有助于!
I created a custom dialog with buttons, but when I go to launch it, my app force closes. I had the dialog working earlier without custom buttons. And am not sure where I am messing up here:
public class BeerPage extends ActionbarMenu {
BeerData e;
Dialog dialog = null;
AlertDialog.Builder builder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beer_page);
builder = new AlertDialog.Builder(this);
//get data from listview
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
//test shared prefs
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
//set image
ImageView im1 = (ImageView) findViewById(R.id.image);
if(e.largeLabel.equals("N/A")){
//set image as png
im1.setImageResource( R.drawable.noimage);
}
else{
ImageDownloadTask imageD = new ImageDownloadTask(im1);
imageD.execute(e.largeLabel);
}
//check if user has beer
String url = "myURL";
String userURLComp = "u=" + userID;
String beerID = "&b=" + e.beerId;
url = url + userURLComp + beerID;
new CheckBeerJSON(this,e.beerId).execute(url);
String url2 = "myURL2";
new GetBeerRateJSON(this,e.beerId).execute(url2);
//inflate loder image
LayoutInflater mInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout addButton = (LinearLayout)((Activity) this).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.checkingportfolio, null));
//prepare buttons
//Button buttonBrewery = (Button) findViewById(R.id.buttonBrewery);
//Button buttonStyle = (Button) findViewById(R.id.buttonStyle);
//prepare text things
TextView tv1 = (TextView) findViewById(R.id.beerTitle);
TextView tv2 = (TextView) findViewById(R.id.beerDescription);
TextView tv_ibu = (TextView) findViewById(R.id.IBU);
TextView tv_abv = (TextView) findViewById(R.id.abv);
TextView tv_glass = (TextView) findViewById(R.id.glass);
TextView tv_breweryName = (TextView) findViewById(R.id.beerBreweryName);
TextView tv_styleName = (TextView) findViewById(R.id.beerStyleName);
//set text thinsg
tv1.setText(e.beerName);
tv2.setText(e.beerDescription);
String breweryButton = "Brewery: ";
String styleButton = "Style: ";
tv_breweryName.setText(e.beerBreweryName);
tv_styleName.setText(e.beerStyle);
breweryButton = breweryButton + e.beerBreweryName;
styleButton = styleButton + e.beerStyle;
//buttonBrewery.setText(breweryButton);
//buttonStyle.setText(styleButton);
tv_ibu.setText(e.beerIBU);
tv_abv.setText(e.beerABV);
tv_glass.setText(e.beerGlass);
//Toast.makeText(this, e.mediumLabel, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
//view brewery function
public void viewBrewery(View view) {
// launch new brewery page class
Intent i = new Intent(this, BreweryPage.class);
i.putExtra("myBeerObject", e);
i.setClass(this, BreweryPage.class);
startActivity(i);
}
public void viewStyle(View view) {
// launch new brewery page class
Intent i = new Intent(this, BreweryPage.class);
i.putExtra("myBeerObject", e);
i.setClass(this, StylePage.class);
startActivity(i);
}
//view brewery function
public void viewTasteTags(View view) {
// launch new brewery page class
Intent i = new Intent(this, BreweryPage.class);
i.putExtra("myBeerObject", e);
i.setClass(this, TasteTags.class);
startActivity(i);
}
public String encodeThisWord(String word){
try {
word = URLEncoder.encode(word, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return word;
}
public void addBeer(View view){
//get user info
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
//get beer details
String url = "http://www.beerportfolio.com/app_addBeer.php?";
String urlUserID = "u=" + userID;
String urlBeerID = "&bID=" + e.beerId;
String urlBeerName = "&bName=" + encodeThisWord(e.beerName);
//construct url for adding beer
url = url + urlUserID + urlBeerID + urlBeerName;
Log.d("url", url);
//execute async on url to add to brewery
new AddBeer(this).execute(url);
//to do: change to start rater
LinearLayout ll = (LinearLayout) findViewById(R.id.addBeerLayout);
ll.removeAllViews();
//add rater
LayoutInflater inflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
LinearLayout addButton = (LinearLayout)this.findViewById(R.id.addBeerLayout);
addButton.addView(inflater.inflate(R.layout.addrate_layout, null));
addListenerOnRatingBar(this);
}
private void addListenerOnRatingBar(BeerPage beerPage) {
RatingBar ratingBar = (RatingBar) this.findViewById(R.id.beerRatingBar);
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
//next async task to update online database
float stars = ratingBar.getRating();
//get user details
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(BeerPage.this);
String userID = prefs.getString("userID", null);
//get beer id
String beerID = e.beerId;
//get rating
String urlRate = "r=" + String.valueOf(ratingBar.getRating());
String urlUserID = "&u=" + userID;
String urlBeerID = "&b=" + beerID;
//construct url
String url2 = "http://www.beerportfolio.com/app_rateUpdate.php?";
url2 = url2 + urlRate + urlUserID + urlBeerID;
Log.d("addRateing", url2);
//async task to update rating in database
new UpdateRating(BeerPage.this).execute(url2);
}
});
}
public void rateDialog(View v){
// custom dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Add the buttons
builder.setPositiveButton("save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.setContentView(R.layout.rate_stars);
dialog.setTitle("Rate this Beer");
dialog.show();
}
}
The force close error I am getting is:
09-26 20:37:43.479 29047-29047/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3609)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17126)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5536)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3604)
... 11 more
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:322)
at com.android.internal.app.AlertController.installContent(AlertController.java:242)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:364)
at android.app.Dialog.show(Dialog.java:261)
at com.example.beerportfoliopro.BeerPage.rateDialog(BeerPage.java:326)
... 14 more
解决方案
@codeMagic is close, but the full answer, cribbed from this answer, is to do this:
// Create the AlertDialog
builder.setTitle("Rate this Beer");
builder.setView(getLayoutInflater().inflate(R.layout.rate_stars, null));
AlertDialog dialog = builder.create();
// DELETE dialog.setContentView(R.layout.rate_stars);
// DELETE dialog.setTitle("Rate this Beer");
dialog.show();
Hope this helps!
这篇关于打开定制对话框时强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!