开发者

Help with AlertDialog in Android

The application seems to crash when I try and get the text from the EditText:

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...
}
}


it will be crashing due to a thing that you will calling this method from a thread other then UI Main Worker thread.

For debugging purpose try calling this method in the very beginining of app in the main class by main thread.

If it appears then make your threading issue clear in the app. Use Handler class object to reslove such issues. I will put some more light if you need


The problem is that height1 is not found within the dialog. Really, you shouldn't seearch one within the dialog, because it belongs not to the dialog, but to the activity. Try this:

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

It will search for the R.id.h1 within your activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜