gpt4 book ai didi

java - 需要更多关于如何将文本设置为 Fragment 内的 TextView 的说明

转载 作者:行者123 更新时间:2023-11-29 21:39:23 25 4
gpt4 key购买 nike

我遇到了和这个问题一样的问题

Change TextView inside Fragment

并且那里写的解决方案工作正常这是解决方案

  import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}


public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.detailsText);
textView.setText(text);
}

}

我的第一个问题 -> 为什么每当我尝试以这种方式在位于 fragment 内的 TextView 中设置文本时,我的应用程序就会崩溃

在类 Main-Activity 的方法中

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

第二个问题->在方法setText的解决方案中为什么我不能写这个

TextView textView = (TextView) getView().findViewById(R.id.detailsText);
textView.setText(text);

   TextView textView = (TextView)findViewById(R.id.detailsText);
textView.setText(text);

当我尝试设置文本时记录 Cat(仅错误)

  07-08 18:40:33.462: E/AndroidRuntime(27881): FATAL EXCEPTION: main
07-08 18:40:33.462: E/AndroidRuntime(27881): java.lang.IllegalStateException: Could not execute method of the activity
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.view.View$1.onClick(View.java:3071)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.view.View.performClick(View.java:3538)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.view.View$PerformClick.run(View.java:14330)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.os.Handler.handleCallback(Handler.java:608)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.os.Looper.loop(Looper.java:156)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.app.ActivityThread.main(ActivityThread.java:4987)
07-08 18:40:33.462: E/AndroidRuntime(27881): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:40:33.462: E/AndroidRuntime(27881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-08 18:40:33.462: E/AndroidRuntime(27881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-08 18:40:33.462: E/AndroidRuntime(27881): at dalvik.system.NativeStart.main(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881): Caused by: java.lang.reflect.InvocationTargetException
07-08 18:40:33.462: E/AndroidRuntime(27881): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:40:33.462: E/AndroidRuntime(27881): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:40:33.462: E/AndroidRuntime(27881): at android.view.View$1.onClick(View.java:3066)
07-08 18:40:33.462: E/AndroidRuntime(27881): ... 11 more
07-08 18:40:33.462: E/AndroidRuntime(27881): Caused by: java.lang.NullPointerException
07-08 18:40:33.462: E/AndroidRuntime(27881): at com.alisaeed.newramadan.MainActivity.question_displayer(MainActivity.java:64)
07-08 18:40:33.462: E/AndroidRuntime(27881): ... 14 more

新代码

public void  question_displayer(View v){


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


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

TextView tv = (TextView)findViewById(R.id.textView_question);



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);

tv.setText(question);

flag_autoquestion++;
}




}



}


}

最佳答案

在 fragment 中,您将调用这样的 TextView

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
String text = "sample text";
textView.setText(text);
return view;
}

设置文本。另一方面,在 Activity 课上会是这样

setContentView(R.activity_layout);
TextView tv = (TextView) findViewById(R.id.textView_question);
tv.setText(question);

关于java - 需要更多关于如何将文本设置为 Fragment 内的 TextView 的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17528259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com