0

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

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 类

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>

任何帮助将不胜感激..................我是这个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;} 
                    }
                         }
4

1 回答 1

1

第二次尝试...

如果一切都失败了,可以 在打电话getSupportFragmentManager().executePendingTransactions() 之前尝试打电话

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

片段事务是线程化的,因此受调度程序的支配。如果您需要立即进行交易,您可以调用上述方法。

于 2013-07-08T22:51:14.670 回答