Iam遇到与此问题相同的问题
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);
}
}
我的第一个问题 - > 为什么每当我尝试在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++;
}
}
}
}
答案 0 :(得分:1)
在片段中,您将调用这样的文本视图
@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;
}
设置文字。另一方面,在活动类中它将是这样的
setContentView(R.activity_layout);
TextView tv = (TextView) findViewById(R.id.textView_question);
tv.setText(question);
答案 1 :(得分:1)
在课程描述中添加:
TextView textView;
在onCreateView(...)中定义TextView
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
您可以从此课程的任何地方使用textView。