以下是我自己编写自定义对话框的活动代码:
package com.example.hotel_app_regularuser;
import android.animation.ObjectAnimator;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class GetUserPreference extends Activity implements Runnable {
TextView tv,tr;
Button bt;
Thread t1;
Dialog d;
ObjectAnimator textColorAnim;
LinearLayout lyt,lyt2;
PopupWindow popUp;
LayoutParams params;
Button cbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_user_preference);
tv=(TextView)findViewById(R.id.textView1);
lyt= (LinearLayout) findViewById(R.id.LinearLayout1);
lyt2=new LinearLayout(this);
popUp = new PopupWindow(this);
t1=new Thread(this);
cbutton=(Button)findViewById(R.id.closebutton);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/rs.ttf");
Intent it=getIntent();
String a=it.getStringExtra("username");
tv.setText("Welcome"+" "+a);
tv.setTypeface(type);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,50);
tv.setTextColor(Color.RED);
d = new Dialog(this);
bt=(Button)findViewById(R.id.button1);
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
// bt.startAnimation(animation);
bt.startAnimation(animation);
d.setContentView(R.layout.customxml);
d.setTitle("welcome");
TextView t = (TextView)d.findViewById(R.id.textView1);
t.setText("hiii");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.get_user_preference, menu);
return true;
}
public void go(View vw)
{
d.show();
}
public void rempopup(View v)
{
d.dismiss();
}
@Override
public void run(){}
}
这是我的customxml.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/closebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="rempopup"/>
</LinearLayout>
问题是,当我点击自定义对话框中的按钮时,我的应用程序突然关闭。由于某种原因,不会调用dismiss()。请帮忙
这是我的崩溃日志
07-04 10:57:29.340: E/AndroidRuntime(19455): FATAL EXCEPTION: main
07-04 10:57:29.340: E/AndroidRuntime(19455): Process: com.example.hotel_app_regularuser, PID: 19455
07-04 10:57:29.340: E/AndroidRuntime(19455): java.lang.IllegalStateException: Could not find a method rempopup(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'closebutton'
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.view.View$1.onClick(View.java:4034)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.view.View.performClick(View.java:4802)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.view.View$PerformClick.run(View.java:20101)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.os.Handler.handleCallback(Handler.java:810)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.os.Looper.loop(Looper.java:189)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.app.ActivityThread.main(ActivityThread.java:5532)
07-04 10:57:29.340: E/AndroidRuntime(19455): at java.lang.reflect.Method.invoke(Native Method)
07-04 10:57:29.340: E/AndroidRuntime(19455): at java.lang.reflect.Method.invoke(Method.java:372)
07-04 10:57:29.340: E/AndroidRuntime(19455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
07-04 10:57:29.340: E/AndroidRuntime(19455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
07-04 10:57:29.340: E/AndroidRuntime(19455): Caused by: java.lang.NoSuchMethodException: rempopup [class android.view.View]
07-04 10:57:29.340: E/AndroidRuntime(19455): at java.lang.Class.getMethod(Class.java:664)
07-04 10:57:29.340: E/AndroidRuntime(19455): at java.lang.Class.getMethod(Class.java:643)
07-04 10:57:29.340: E/AndroidRuntime(19455): at android.view.View$1.onClick(View.java:4027)
07-04 10:57:29.340: E/AndroidRuntime(19455): ... 10 more
答案 0 :(得分:1)
试试这个:
<强>被修改强>
final Dialog d = new Dialog(this);
Button Button = (Button) d.findViewById(R.id.closebutton);
Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
在您的.xml文件中
<Button
android:id="@+id/closebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
希望这会有所帮助:)
答案 1 :(得分:1)
public class GetUserPreference extends Activity implements Runnable {
TextView tv, tr;
Button bt;
Thread t1;
Dialog d;
ObjectAnimator textColorAnim;
LinearLayout lyt, lyt2;
PopupWindow popUp;
LayoutParams params;
Button cbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_user_preference);
tv = (TextView) findViewById(R.id.textView1);
lyt = (LinearLayout) findViewById(R.id.LinearLayout1);
lyt2 = new LinearLayout(this);
popUp = new PopupWindow(this);
t1 = new Thread(this);
cbutton = (Button) findViewById(R.id.closebutton);
Typeface type = Typeface.createFromAsset(getAssets(), "fonts/rs.ttf");
Intent it = getIntent();
String a = it.getStringExtra("username");
tv.setText("Welcome" + " " + a);
tv.setTypeface(type);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
tv.setTextColor(Color.RED);
d = new Dialog(this);
bt = (Button) findViewById(R.id.button1);
final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
// bt.startAnimation(animation);
bt.startAnimation(animation);
d.setContentView(R.layout.customxml);
d.setTitle("welcome");
TextView t = (TextView) d.findViewById(R.id.textView1);
t.setText("hiii");
Button close_btn = (Button) d.findViewById(R.id.closebutton);
close_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.get_user_preference, menu);
return true;
}
public void go(View vw) {
d.show();
}
// public void rempopup(View v) {
// d.dismiss();
// }
@Override
public void run() {
}
}
也删除
android:onClick="rempopup"
答案 2 :(得分:0)
试试这个:
public void rempopup(View v)
{
if(v.getId() == R.id.closebutton)
d.dismiss();
}