android - 将字符串片段传递给片段时出错

时间:2016-07-12 18:51:23

标签: android android-fragments

所以当点击一个按钮时,我试图将字符串从frag_one传递到frag_two,并且它在MaiM.java:89和StartDialog.java:56

时崩溃错误

MainM

public void onSentText(String text) {
ThreeFragment threeFragment =(ThreeFragment)getSupportFragmentManager().findFragmentByTag("fragthree");
 threeFragment.text.setText(text); //here:89
}

FragmentNumberTwo

public class ThreeFragment extends Fragment {
TextView text;
String stringtext;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_three, container, false);
text = (TextView)view.findViewById(R.id.test);
return view;
}

public void setText(final String string){
text.setText(stringtext);
}
public void sentText(){
new MyTask().execute();
}

private class MyTask extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... strings){
Bundle b = getArguments();
stringtext = b.getString("text");
return null;
}
protected void onPostExecute(String result){
setText(stringtext);
}
}
}

FragmentNumberOne 只是按钮/ onClick写的声明

StartDialog

public class StartDialog extends DialogFragment {
OnFragmentSentText mSendText;
String send_text;
Button start;

public interface OnFragmentSentText{
public void onSentText(String text);
}

public void onAttach(Activity activity){
super.onAttach(activity);
try{
mSendText = (OnFragmentSentText)activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString()+"implement OnFragmentSentText");
}
}

@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
start = (Button)getActivity().findViewById(R.id.btnStart);
builder.setTitle("START");
builder.setMessage("Are You Sure ?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

send_text = start.getText().toString();
mSendText.onSentText(send_text);//here:56

start.setTextColor(Color.BLACK);
start.setEnabled(false);
Toast.makeText(getContext(), "Command Sent!", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
 @Override
public void run() {
start.setEnabled(true);
start.setTextColor(Color.WHITE);
}
},10000);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}

方案:
当我单击FragmentNumberOne中的按钮时,将显示一个AlertDialog,当我单击肯定按钮时,它将按钮文本作为字符串,并将放置在FragmentNumberTwo中的TextView上。我把“//”放在错误的位置

07-13 02:26:03.618 22983-22983/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.thesis.ls.mobile, PID: 22983
                                               java.lang.NullPointerException
                                                   at com.thesis.ls.mobile.MainM.onSentText(MainM.java:89)
                                                   at com.thesis.ls.mobile.StartDialog$1.onClick(StartDialog.java:56)
                                                   at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:149)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                   at dalvik.system.NativeStart.main(Native Method)

fragment_three.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="fragthree"
tools:context="com.thesis.ls.mobile.ThreeFragment"
android:background="#d5d3d3">


<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/test"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

0 个答案:

没有答案