此代码段有什么问题....我的应用程序在此处崩溃

frag.settext("Default Messaage");  // dEBUGGER SAYS THE PROBLEM IS HERE  IT THROWS NPE


代码段

public void  question_displayer(View v){


    Log.i("create","question_displayer()");
    RamadanData data_object = new RamadanData();


   fragment_question_displayer at = new fragment_question_displayer();
    FragmentTransaction fragment_question = getSupportFragmentManager().beginTransaction().add(at, "why");
    fragment_question.replace(R.id.framelayout1, new fragment_question_displayer()  ,null);
    fragment_question.addToBackStack(null);
    fragment_question.commit();



   fragment_question_displayer  frag = (fragment_question_displayer)getSupportFragmentManager().findFragmentByTag("why");
   frag.settext("Default Messaage"); // dEBUGGER SAYS THE PROBLEM IS HERE  IT THROWS NPE


fragment_question_displayer CLASS

package com.alisaeed.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.TextView;

import com.alisaeed.newramadan.R;

public class fragment_question_displayer extends Fragment {
   Context context;
   TextView question_displayer;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        context = inflater.getContext();
        View v= inflater.inflate(R.layout.question_displayer, container, false);
    //  TextView question_displayer = (TextView)v.findViewById(R.id.textView_question );
    //  question_displayer.setText("Default Message");
        return v;
    }

 public void settext(String question){

          question_displayer = (TextView)getView().findViewById(R.id.textView_question);
        question_displayer.setText(question);

     }


}


question_displayer 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Question_displayer"
    android:tag="Hello"
     >

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="36dp"
        android:text="D" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button4"
        android:layout_alignLeft="@+id/button2"
        android:layout_marginBottom="15dp"
        android:text="C" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="25dp"
        android:text="B" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/button2"
        android:layout_marginBottom="16dp"
        android:text="A" />

    <TextView
        android:id="@+id/textView_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


任何帮助将不胜感激............ Iam初学者在此android编程

斯科特

public void  question_displayer(View v){


Log.i("create","question_displayer()");
    RamadanData data_object = new RamadanData();


   fragment_question_displayer at = new fragment_question_displayer();
    FragmentTransaction fragment_question = getSupportFragmentManager().beginTransaction().add(android.R.id.content,at, "why");
    fragment_question.replace(R.id.framelayout1, new fragment_question_displayer()  ,null);
    fragment_question.addToBackStack(null);
    fragment_question.commit();
   getSupportFragmentManager().executePendingTransactions();
   fragment_question_displayer  frag = (fragment_question_displayer)getSupportFragmentManager().findFragmentByTag("why");


   if(v.getId()==R.id.buttonnext)
   {
        String q = data_object.question_output(flag_autoquestion,"child","prayer" );
        frag.settext(q);


     }


    switch(v.getId()){
    case (R.id.buttonPrayer):
    {


        Log.i("create","prayer()");

        if(child_selected == true && adult_selected==false && teen_selected==false )
        {
            Log.i("create","child_selected");

           String question= data_object.question_output(flag_autoquestion, "child","prayer");
           Log.i("create",question);
           frag.settext(question);


           flag_autoquestion++;
                                   }
           break;}
                    }
                         }

最佳答案

第二次尝试...

如果其他所有方法都失败,请尝试致电
getSupportFragmentManager().executePendingTransactions()
致电之前

fragment_question_displayer frag = (fragment_question_displayer)getSupportFragmentManager() .findFragmentByTag("why");

碎片事务被清除,因此受调度程序的支配。如果您需要立即进行交易,则可以调用上述方法。

关于java - 为 fragment 设置字符串标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17536979/

10-10 02:41