leyna1011
leyna1011
2015-09-08 11:52

android获取Dialog中edittext内容时空指针异常求解决

  • android
  • 异常

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

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

    public void on(View view){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("title");
        LayoutInflater inflater = getLayoutInflater();
        final View v = inflater.inflate(R.layout.diy, null);
        dialog.setView(v);
        dialog.setPositiveButton("text", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText editText = (EditText) findViewById(R.id.editText1);
                String string = editText.getText().toString();
                Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }
}

错误提示:32行空指针异常,看不出拿了错了。。。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答