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行空指針異常,找了好久也沒有找到哪裡錯了。。。
Android中的getLastKnowLocation空指針異常
Android中findViewById()獲取EditText 空指針問題
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!