gpt4 book ai didi

java - 带有全局变量的 TextView

转载 作者:行者123 更新时间:2023-12-02 09:00:47 24 4
gpt4 key购买 nike

我只是在 Textview 对象上调用全局变量并将其打印出来,但出现以下错误

如何才能正常运行下面的代码?

p.s.textread_buf 是全局变量。

<小时/>

日志猫

E/AndroidRuntime:致命异常:main 进程:com.example.sentence_app,PID:26177

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

at com.example.sentence_app.senten_check_example_mean.onCreateView(senten_check_example_mean.java:40)
<小时/>

//错误代码

public class senten_check_example_mean extends Fragment {

public static senten_check_example_mean newInstance(){

return new senten_check_example_mean();

}

public senten_check_example_mean() {
// Required empty public constructor
}

Button return_sentence_check_button;

TextView textView_name ;
TextView textView_mean ;
TextView textView_example ;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment

textView_name =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_name);
textView_mean =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_mean);
textView_example =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_example);

textView_name.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_name());
textView_mean.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_example());
textView_example.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_mean());

return inflater.inflate(R.layout.fragment_senten_check_example_mean, container, false);
}

}

//textread.class

package com.example.sentence_app;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

public class textread {

String sentence_name;
String sentence_example;
Queue<String> sentence_mean;

textread(){

}

textread(String temp){
String []cs = temp.split("\\|");
this.setSentence_name(cs[0]);
this.setSentence_mean(cs[1]);
this.setSentence_example(cs[2]);
}

public String getSentence_example() {
return sentence_example;
}

public String getSentence_mean() {
return sentence_mean.peek();
}

public String getSentence_name() {
return sentence_name;
}


}

最佳答案

这个问题最终将作为空指针异常的重复问题被关闭。但是,如果这些 TextView 像平常一样位于您的 fragment 布局内,那么请尝试重构您的 fragment onCreateView,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_senten_check_example_mean, container, false);

textView_name =(TextView) view.findViewById(R.id.text_read_name);
textView_mean =(TextView) view.findViewById(R.id.text_read_mean);
textView_example =(TextView) view.findViewById(R.id.text_read_example);

textView_name.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_name());
textView_mean.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_example());
textView_example.setText(((MyApplication) getActivity().getApplicationContext()).textread_buf.getSentence_mean());

return view;
}

您需要首先膨胀布局 View ,然后在该膨胀布局中找到您的 View 。在 Activity 中查找这些 View 不会返回任何内容。

关于java - 带有全局变量的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60181582/

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