2013-06-23 5 views
0

J'ai 2 boutons dans mon fichier xml, onclicklistener open new popup.why this eror? peut m'aider ??2 bouton événement OnClick sur la fenêtre popup dans Android

code Java Mon est la suivante:

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> 

Quelqu'un peut-il me aider à connaître la raison de ce problème et comment puis-je résoudre. Je suis un débutant à Android.

Merci

+2

Quelle est l'erreur? – Plato

+0

je ne sais pas ,, mais sa force fermer si j'ouvre cette activité –

Répondre

1

essayer cette

import android.os.Bundle; 
import android.app.Activity; 
import android.app.Dialog; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

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

    Button bt_1 = (Button) findViewById(R.id.bt1); 
    bt_1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      final Dialog dialog = new Dialog(MainActivity.this); 
      dialog.setContentView(R.layout.activity_popup); 
      dialog.setTitle("Hans Kelsen"); 

      //Her add your textView and ImageView if you want 

      Button dialogButton = (Button) dialog.findViewById(R.id.bt1_popUP); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
       dialog.dismiss(); 
       } 
      }); 
      dialog.show(); 
     } 
     }); 

     // Same thing for bt_2 

    } 

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

} 

Votre Activité principale XML

<RelativeLayout 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: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=".MainActivity" > 

    <Button 
     android:id="@+id/bt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="BT 1" /> 

    <Button 
     android:id="@+id/bt2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/bt1" 
     android:layout_marginTop="40dp" 
     android:text="BT 2" /> 

</RelativeLayout> 

Votre POP-UP XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="#FFF123" 
    android:gravity="center" > 

    <Button 
     android:id="@+id/bt1_popUP" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Close" /> 

</RelativeLayout> 

enjoy :)

+0

c'est du travail: D .. merci ,,, –

Questions connexes