繁体   English   中英

片段视图返回空

[英]Fragment View Return Null

即使我已将视图设置为全局变量并在 onCreateView() 上调用 findViewById,我的视图也一直返回 null。

这是我的视图部分代码

public class MainMenu extends Fragment {
TextView txt = null;
View view1;
View mView;


public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
   // view1 = inflater.inflate(R.layout.fragment_main_menu, container, false);
    mView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_main_menu, null,false);
    pref = getActivity().getPreferences(0);
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(pref.getString("CONSUMER_KEY", ""), pref.getString("CONSUMER_SECRET", ""));
    txt = (TextView)mView.findViewById(R.id.tww1);


    //run retrieve tweets method
    new TweetStream().execute();

    new Thread(new MainMenu().new Consumer()).start();

    return mView;
}

在我的 fragment_main_menu.xml

  <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tww1"
                android:gravity="center"
                android:textSize="100dp"
                android:text="OFF"/>

        </LinearLayout>

以及调用它的代码:

public void turnOn(){
txt.setText("ON");
}

运行 turnOn 函数的方法:

 class Consumer implements Runnable {

    @Override
    public void run() {
        try {

            while (queue.size() != 0) {
                String msg = getObj.getMsg();
                String[] result = msg.split("\\s");
                String msg2 = result[0];
                if(msg2.equals("ON")){
                    txt.setText("ON");
                    //turnOn();
                }
                else if(msg.equals("OFF")){
                    txt.setText("OFF");
                   // turnOff();
                }

            }
            if (queue.size() == 0) {
                Thread.sleep(1000);
                run();
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

这是我得到的错误:

06-19 12:47:43.921    5213-9586/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-128222
Process: com.example.john.fabric, PID: 5213
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

有任何想法吗? 谢谢!

编辑 1:我对 inflater inflate 方法使用 false。 我不明白为什么我应该得到一个空指针。

Edit2:在里面运行 turnOn 或 setText 方法都返回 null。

使用它来膨胀布局,然后检查

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view1 = inflater.inflate(R.layout.fragment_main_menu, container, false);
    txt = (TextView) view1.findViewById(R.id.tww1);
    new Thread(new Consumer()).start();
    return view1;
}

class Consumer implements Runnable {

    @Override
    public void run() {
        try {

            while (queue.size() != 0) {
                String msg = getObj.getMsg();
                String[] result = msg.split("\\s");
                String msg2 = result[0];
                if (msg2.equals("ON")) {
                    turnOn();
                } else{
                    txt.setText("OFF");
                    // turnOff();
                }

            }
            if (queue.size() == 0) {
                Thread.sleep(1000);
                run();
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public void turnOn(){
    txt.setText("ON");
}

使用 runonUi 线程将值更新到 UI 部分尝试下面的代码

@Override
    public void run() {
        try {

            while (queue.size() != 0) {
                String msg = getObj.getMsg();
                String[] result = msg.split("\\s");
                String msg2 = result[0];

          runOnUiThread(new Runnable() {

                @Override
                public void run() {
                if(msg2.equals("ON")){
                    txt.setText("ON");
                    //turnOn();
                }
                else if(msg.equals("OFF")){
                    txt.setText("OFF");
                   // turnOff();
                }
              }
             }
            }
            if (queue.size() == 0) {
                Thread.sleep(1000);
                run();
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM