我有类似的东西:
public class EditCustomer extends Fragment implements OnClickListener {
public void onClick(View v) {
//this is what runs when you click the button
EditText editName=(EditText) v.findViewById(R.id.edit_name);
System.out.println(editName.getText()); //FAILS
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment4
View v=inflater.inflate(R.layout.fragment_edit_customer, container, false);
Button a = (Button) v.findViewById(R.id.update_cust_btn);
a.setOnClickListener(this); //adds button listener
EditText editText = (EditText)v.findViewById(R.id.edit_name);
System.out.print(editText.getText().toString());
return v;
}
...
出于某种原因,EditText中的空指针异常。当我在同一个片段中的onCreateView()
中尝试同一行时,它的工作正常。有什么我不能来这里吗?
答案 0 :(得分:0)
第1步导入这两个包。
import android.view.View;
import android.view.View.OnClickListener;
第2步在您的班级中实施 OnClickListener
public class SettingsFragment extends Fragment implements OnClickListener
第3步在onCreateView()中定义Button和EditText;
注意: - 全局声明Button和EditText
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_settings, container,
false);
Admin_Email = (EditText) rootView.findViewById(R.id.et_admin_email);
btn_SET = (Button) rootView.findViewById(R.id.btn_SET);
btn_ChangePassword = (Button) rootView
.findViewById(R.id.btn_ChangePassword);
btn_SET.setOnClickListener(this);
btn_ChangePassword.setOnClickListener(this);
return rootView;
}
第4步在OnCreateView()之外使用Id定义Onclick()方法
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_SET:
Toast toast = Toast.makeText(getActivity(), ""+Admin_Email.getText().toString(),
Toast.LENGTH_SHORT);
break;
case R.id.btn_ChangePassword:
break;
default:
break;
}
}
我的完整代码
mport android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Patterns;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.regex.Pattern;
public class SettingsFragment extends Fragment implements OnClickListener {
Button btn_SET, btn_ChangePassword;
static EditText Admin_Email;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_settings, container,
false);
Admin_Email = (EditText) rootView.findViewById(R.id.et_admin_email);
btn_SET = (Button) rootView.findViewById(R.id.btn_SET);
btn_ChangePassword = (Button) rootView
.findViewById(R.id.btn_ChangePassword);
btn_SET.setOnClickListener(this);
btn_ChangePassword.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_SET:
Toast toast = Toast.makeText(getActivity(), ""+Admin_Email.getText().toString(),
Toast.LENGTH_SHORT);
break;
case R.id.btn_ChangePassword:
//
break;
default:
break;
}
}
}