2011-02-12 31 views
0

的應用似乎崩潰時,我嘗試並獲得從EditText上的文字:幫助AlertDialog中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

在「確定」按鈕處理之前,嘗試打印height1.getText()。toString()的輸出,這是什麼意思? – TheCottonSilk 2011-02-12 09:37:33

回答

0

將會崩潰,由於你會調用此方法從其他線程,然後一件事UI主工作者線程。

爲了調試目的,請嘗試在主線程的主類中的應用程序的初始階段調用此方法。

如果它出現,然後讓你的線程問題清楚的應用程序。 使用Handler類對象來解決此類問題。如果需要,我會再放一些燈

+0

你能否進一步解釋?我已更新我的代碼以提供幫助。 – Biggsy 2011-02-12 18:54:03

0

問題是在對話框中找不到height1。真的,你不應該在對話框中找到一個,因爲它不屬於對話框,而是屬於活動。試試這個:

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

它會在您的活動中搜索R.id.h1。