【问题标题】:Textview with global variable具有全局变量的文本视图
【发布时间】:2020-02-12 05:18:15
【问题描述】:

我只是简单地调用了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;
    }


}

【问题讨论】:

  • 在 onViewCreated 中执行相同的实现。您将在 onViewCreated 中获取视图作为参数,使用它来初始化视图。像 view.findViewById(R.id.text_read_name);这会工作
  • 像 id text_read_name 这样的文本视图在您的片段布局中 fragment_senten_check_example_mean 对吗?

标签: java android nullpointerexception


【解决方案1】:

这个问题最终将作为空指针异常的重复而被关闭。但是,如果这些文本视图像正常一样在您的片段布局中,那么请尝试重构您的片段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;
    }

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

【讨论】:

    【解决方案2】:

    首先您将初始化 onCreateView 的 textview 更改为 onViewCreated 方法,因为它不好练习,并且有时 View 没有膨胀,所以在 onViewCreated 方法中初始化您的变量,然后重新编译代码。

    【讨论】:

    • 这样吗? public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 膨胀这个片段的布局 TextView textView_name =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_name); TextView textView_mean =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_mean); TextView textView_example =(TextView) ((MainActivity)getActivity()).findViewById(R.id.text_read_example);
    • @Marok 你能分享完整的错误吗?我的意思是详细说明
    【解决方案3】:

    首先,将你的 findViewbyId() 移动到 onViewCreated

    其次,它不起作用,因为您使用 getActivity()(获取父活动)在片段 xml 中查找视图。使用 getView().findViewById() 或使用 inflater 代替

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2014-05-05
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多