My view has been returning null even though i have set the view as a global variable and calling the findViewById on the onCreateView().
This are my codes for the view part
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;
}
In 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>
and the codes that invoke it:
public void turnOn(){
txt.setText("ON");
}
the method which runs the turnOn function:
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();
}
}
}
and this is the error i get:
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
Any ideas? Thank you!
Edit1: I am using false for the inflater inflate method. I do not see why i should get a null pointer.
Edit2: Running turnOn or setText method inside both return me null.
Use this to inflate layout and then check
@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");
}
Use runonUi thread to update values to UI part try below code
@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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.