2011-02-12 5 views
0

L'application semble se bloquer lorsque je tente et obtenir le texte de la EditText:Aide AlertDialog dans Android

package com.example.helloandroid; 
import android.content.Intent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.util.Log; 

import android.app.Activity; 
import android.os.Bundle; 

public class MatrixMultiply extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    View newButton = findViewById(R.id.new_button); 
    newButton.setOnClickListener(this); 
    View aboutButton = findViewById(R.id.about_button); 
    aboutButton.setOnClickListener(this); 
    View exitButton = findViewById(R.id.exit_button); 
    exitButton.setOnClickListener(this); 
} 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.about_button: 
     Intent i = new Intent(this, About.class); 
     startActivity(i); 
     break; 
    case R.id.new_button: 
     openNewGameDialog(); 
     break; 
    case R.id.exit_button: 
     finish(); 
     break; 
// More buttons go here (if any) ... 
} 
} 
private static final String TAG = "Matrix"; 

private void openNewGameDialog() { 

    LayoutInflater factory = LayoutInflater.from(this);    
     final View textEntryView = factory.inflate(R.layout.text, null); 

     AlertDialog.Builder alert = new AlertDialog.Builder(this); 

     alert.setTitle("Matrices"); 
     alert.setMessage("Please enter the size of the matrix"); 
     // Set an EditText view to get user input 
     alert.setView(textEntryView); 
     AlertDialog matrixSize = alert.create(); 

     final EditText height1 = (EditText) matrixSize.findViewById(R.id.h1); 
     final EditText width1 = (EditText) MatrixMultiply.this.findViewById(R.id.w1); 
     final EditText height2 = (EditText) MatrixMultiply.this.findViewById(R.id.h2); 
     final EditText width2 = (EditText) MatrixMultiply.this.findViewById(R.id.w2); 


     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      String h1 = height1.getText().toString(); 
      String w1 = width1.getText().toString(); 
      String h2 = height2.getText().toString(); 
      String w2 = width2.getText().toString(); 


     } 
     }); 

     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
      // Canceled. 
      } 
     }); 

     alert.show(); 
} 


private void startGame(int i) { Log.d(TAG, "clicked on " + i); // Start game here... 
} 
} 
+0

Essayez d'imprimer la sortie de height1.getText(). ToString() avant le traitement du bouton 'Ok', qu'est-ce que cela révèle? – TheCottonSilk

Répondre

0

il sera plantait à cause d'une chose que vous appeler cette méthode à partir d'un autre thread alors UI Main Worker thread. Pour le débogage, essayez d'appeler cette méthode dès le début de l'application dans la classe principale par le thread principal.

S'il apparaît, rendez votre problème de filetage clair dans l'application. Utilisez un objet de classe Gestionnaire pour résoudre ces problèmes. Je vais mettre un peu plus de lumière si vous avez besoin

+0

Pourriez-vous expliquer plus avant? J'ai mis à jour mon code pour aider. – Biggsy

0

Le problème est que height1 est introuvable dans la boîte de dialogue. Vraiment, vous ne devriez pas en chercher un dans le dialogue, car il n'appartient pas au dialogue, mais à l'activité. Essayez ceci:

final EditText height1 = (EditText) YourActivityClassName.this.findViewById(R.id.h1); 

Il recherchera le R.id.h1 dans votre activité.