ボタン付きのカスタム ダイアログを作成しましたが、それを起動しようとすると、アプリが強制終了します。カスタムボタンなしで以前にダイアログを機能させました。そして、私がここでどこを台無しにしているのかわかりません:
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();
}
}
私が得ている強制終了エラーは次のとおりです。
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