我的 xml 文件中有 2 个按钮,onclicklistener open new popup。为什么会出现这个错误?可以帮我??
我的Java代码如下:
Public class Bab1_b1a extends Activity {
final Context context = this;
private Button hans, logemann;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bab1_b1a);
hans = (Button) findViewById(R.id.hans);
logemann = (Button) findViewById(R.id.logemann);
hans.setOnClickListener(myhandler1);
logemann.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_popup_hans);
dialog.setTitle("Hans Kelsen");
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.hans_kelsen);
Button dialogButton = (Button) dialog.findViewById(R.id.back_hans);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_logemann);
dialog.setTitle("Logemann");`enter code here`
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.logemann);
Button dialogButton = (Button) dialog.findViewById(R.id.back_logemann);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
};
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Bab1_b1a" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/nav_back"
android:contentDescription="@string/skx" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="@string/h_neg"
android:textSize="13sp" />
<Button
android:id="@+id/hans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hans_kelsen"
android:textSize="15sp" />
<Button
android:id="@+id/logemann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/logemann" />
</LinearLayout>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/nav_forw"
android:contentDescription="@string/skx" />
</LinearLayout>
谁能帮我知道这个问题的原因以及如何解决它。我是安卓新手。
谢谢