片段视图返回空

时间:2015-06-19 04:48:35

标签: java android

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

这是我的视图部分代码

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

有什么想法吗?谢谢!

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

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

2 个答案:

答案 0 :(得分:1)

使用此选项来扩充布局,然后检查

@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");
}

答案 1 :(得分:1)

使用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();
        }