我收到一个错误,经过数周的努力后,我无法弄明白。
我试图做一个片段的例子,当我点击一个按钮时打开另一个对话框片段。
对话框片段有一个edittext视图,我试图用来输入一个名字然后当我点击确定时,片段假设使用一个接口来完成对调用片段的回调,然后从中获取文本edittext并使用它来更新第一个片段布局中textview中的文本,但无论我做什么它都行不通。
我已经没有错误的想法以及如何解决它。任何帮助将不胜感激。
the compiler says line 72 gives the error in DialogFragment (NullPointerException)
(I put a // line 72 comment on that line)
mListener.setsNewUsername(etEnteredName.getText().toString());// line 72
主要活动
package com.example.fragments;
import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.widget.TextView;
public class FragmentsActivity extends Activity
implements SignInDialogFragment.MyDialogListener{
String sNewUsername = new String("[Enter Username]");
public void setsNewUsername(String sNewUsername) {
this.sNewUsername = sNewUsername;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
TextView textViewFrag1 =(TextView)dialog.getActivity().findViewById(R.id.textViewFragment1);
textViewFrag1.setText("Hello" + sNewUsername);
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
}
主要片段
package com.example.fragments;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.d("Fragment 1", "onCreateView");
return inflater.inflate(R.layout.fragment1, container,false);
}
public void onAttach(Activity activity){
super.onAttach(activity);
Log.d("Fragment 1", "onAttach");
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d("Fragment 1", "onCreate");
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Log.d("Fragment 1", "onActivityCreated");
}
public void onStart(){
super.onStart();
Button btnShowSignin = (Button)getActivity().findViewById(R.id.btnShowSignInDialog);
btnShowSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SignInDialogFragment signinDialog = new SignInDialogFragment();
signinDialog.show(getFragmentManager(), null);
}
});
Log.d("Fragment 1", "onStart");
}
public void onResume(){
super.onResume();
Log.d("Fragment 1", "onResume");
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d("Fragment 1", "onPause");
}
public void onStop(){
super.onStop();
Log.d("Fragment 1", "onStop");
}
public void onDestroyView(){
super.onDestroyView();
Log.d("Fragment 1", "onDestroyView");
}
public void onDestroy(){
super.onDestroy();
Log.d("Fragment 1", "onDestroy");
}
public void onDetach(){
super.onDetach();
Log.d("Fragment 1", "onDetach");
}
}
对话框片段
package com.example.fragments;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class SignInDialogFragment extends DialogFragment {
View dialogView;
EditText etEnteredName;
String enteredUserName = new String("My Name");
public String getEnteredUserName() {
return enteredUserName;
}
public void setEnteredUserName(String enteredUserName) {
this.enteredUserName = enteredUserName;
}
public interface MyDialogListener {
public void setsNewUsername(String string);
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
MyDialogListener mListener;
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (MyDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.signin, null))
.setPositiveButton("SignIn", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
etEnteredName = (EditText)getActivity().findViewById(R.id.username);
mListener.setsNewUsername(etEnteredName.getText().toString()); // line 72
mListener.onDialogPositiveClick(SignInDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
@Override
public void dismiss() {
// TODO Auto-generated method stub
super.dismiss();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
super.onDismiss(dialog);
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.fragments.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</LinearLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<TextView
android:id="@+id/textViewFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SignIn Dialog Text Here"
android:textColor="#000000"
android:textSize="25sp"
/>
<Button
android:id="@+id/btnShowSignInDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show SignIn Dialog"
android:textColor="#000000"
android:onClick="onClick"
/>
</LinearLayout>
signin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
/>
</LinearLayout>
答案 0 :(得分:0)
更改此
etEnteredName = (EditText)getActivity().findViewById(R.id.username);
到
View v = inflater.inflate(R.layout.signin,null);
etEnteredName = (EditText)v.findViewById(R.id.username);
您需要使用膨胀的视图对象来初始化EditText
。 EditText位于R.layout.signin
。 findViewById
查找当前膨胀布局中提到的ID的视图。
将View v;
声明为实例变量。
然后
v = inflater.inflate(R.layout.signin,null);
builder.setView(v);
然后
etEnteredName = (EditText)v.findViewById(R.id.username);