Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 片段视图返回Null_Java_Android - Fatal编程技术网

Java 片段视图返回Null

Java 片段视图返回Null,java,android,Java,Android,即使我已将视图设置为全局变量并在onCreateView()上调用findViewById,我的视图仍返回null 这是视图部分的代码 public class MainMenu extends Fragment { TextView txt = null; View view1; View mView; public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceStat

即使我已将视图设置为全局变量并在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;
}
在my 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>
运行开启功能的方法:

 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:我使用false作为充气机充气方法。我不明白为什么我应该得到一个空指针


Edit2:在这两个函数中运行turnOn或setText方法都会返回空值。

使用此函数对布局进行充气,然后进行检查

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

您在哪里调用了
打开()
?并将所有代码正确地从您开始线程的位置发布?嗨@KrishnaV添加!它也在onCreateViewprovide主菜单类中。发布您的完整代码…嗨@anand,我仍然为空。
if(msg2.equals(“ON”){txt.setText(“ON”);//删除此开启();}
即使我使用开启()或者txt.setText,它将返回空值。这与我最初使用充气器发布的内容不一样吗?
@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();
        }