Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a GUI Update Thread
    text
    copied!<p>I have created a custom ArrayAdapter that displays a listview with images, text, and a data field that can be updated by modifying a set of global variables. I can initialize the interface and verify that by changing the global variables I can change the listview, but only from within OnCreate(). As such, I have tried creating a thread to update the listview every 200ms using notifyDataSetChanged(), but I cannot seem to start the thread. Trying to call <code>.start();</code> creates an error that states "The method start() is undefined for the type MainActivity". Any help would be appreciated.</p> <p><strong>MainActivity</strong></p> <pre><code>public class MainActivity extends Activity { // Sensor Constants public static String temperature; public static String humidity; public static String lpg; public static String alcohol; public static int temperature_int; public static int humidity_int; public static int lpg_int; public static int alcohol_int; // Layout ListView listView; ItemAdapter adapter; // USB UsbManager USB_Manager; UsbDevice Sense; PendingIntent permission; IntentFilter filter; //BroadcastReceiver receiver; //USBBuffer_s_received_data = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initialize Interface Model.LoadModel(); listView = (ListView) findViewById(R.id.listView); String[] ids = new String[Model.Items.size()]; for (int i= 0; i &lt; ids.length; i++) {ids[i] = Integer.toString(i+1);} ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids); listView.setAdapter(adapter); //USB if ((UsbManager) getSystemService(Context.USB_SERVICE) != null) { USB_Manager = (UsbManager) getSystemService(Context.USB_SERVICE); if (USB_Manager.getDeviceList().values().iterator().next() != null) { Sense = USB_Manager.getDeviceList().values().iterator().next(); if ((UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE) != null) { Sense = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE); } } } // Update Layout temperature_int = 30; humidity_int = 6; lpg_int = 5000; alcohol_int = 500; temperature = String.valueOf(temperature_int); humidity = String.valueOf(humidity_int); lpg = String.valueOf(lpg_int); alcohol = String.valueOf(alcohol_int); Model.LoadModel(); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); GUI_Update(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void GUI_Update() { new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(200); Model.LoadModel(); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); } catch (InterruptedException e) { } } } }); //.start(); } </code></pre> <p><strong>EDIT</strong> Moving the location of .start() fixed the problem but now the application exits immediately upon launch and I am not sure why. This is the error log that I get..</p> <pre><code>12-20 17:22:11.010: D/libEGL(15395): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 12-20 17:22:11.010: D/libEGL(15395): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 12-20 17:22:11.018: D/libEGL(15395): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 12-20 17:22:11.096: D/OpenGLRenderer(15395): Enabling debug mode 0 12-20 17:22:11.369: W/dalvikvm(15395): threadid=11: thread exiting with uncaught exception (group=0x4198ac68) 12-20 17:22:11.377: E/AndroidRuntime(15395): FATAL EXCEPTION: Thread-711 12-20 17:22:11.377: E/AndroidRuntime(15395): Process: com.byrdonatwigge.sense, PID: 15395 12-20 17:22:11.377: E/AndroidRuntime(15395): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.View.invalidate(View.java:10942) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.View.invalidate(View.java:10897) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.widget.ImageView.invalidateDrawable(ImageView.java:201) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:344) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.graphics.drawable.Drawable.setVisible(Drawable.java:575) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.widget.ImageView.onDetachedFromWindow(ImageView.java:1243) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.View.dispatchDetachedFromWindow(View.java:12627) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2585) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:4027) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.widget.AbsListView.resetList(AbsListView.java:1924) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.widget.ListView.resetList(ListView.java:521) 12-20 17:22:11.377: E/AndroidRuntime(15395): at android.widget.ListView.setAdapter(ListView.java:462) 12-20 17:22:11.377: E/AndroidRuntime(15395): at com.byrdonatwigge.sense.MainActivity$2.run(MainActivity.java:445) 12-20 17:22:11.377: E/AndroidRuntime(15395): at java.lang.Thread.run(Thread.java:841) 12-20 17:22:12.127: D/AndroidRuntime(15395): Shutting down VM 12-20 17:22:12.127: W/dalvikvm(15395): threadid=1: thread exiting with uncaught exception (group=0x4198ac68) 12-20 17:22:12.127: I/Process(15395): Sending signal. PID: 15395 SIG: 9 </code></pre> <p><strong>EDIT #2</strong> *<em>Update Thread</em>*</p> <pre><code>private void GUI_Update() { new Thread() { public void run() { while (true) { try { runOnUiThread(new Runnable() { @Override public void run() { Model.LoadModel(); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); } }); Thread.sleep(500); } catch (InterruptedException e) { } } } }.start(); } </code></pre> <p><strong>Log</strong></p> <pre><code>12-20 18:49:49.741: D/dalvikvm(17054): Late-enabling CheckJNI 12-20 18:49:50.085: D/libEGL(17054): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 12-20 18:49:50.092: D/libEGL(17054): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 12-20 18:49:50.100: D/libEGL(17054): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 12-20 18:49:50.186: D/OpenGLRenderer(17054): Enabling debug mode 0 12-20 18:50:09.405: D/AndroidRuntime(17117): Shutting down VM 12-20 18:50:09.405: W/dalvikvm(17117): threadid=1: thread exiting with uncaught exception (group=0x4198ac68) 12-20 18:50:09.405: E/AndroidRuntime(17117): FATAL EXCEPTION: main 12-20 18:50:09.405: E/AndroidRuntime(17117): Process: com.byrdonatwigge.sense, PID: 17117 12-20 18:50:09.405: E/AndroidRuntime(17117): java.lang.NullPointerException 12-20 18:50:09.405: E/AndroidRuntime(17117): at com.byrdonatwigge.sense.MainActivity$2$1.run(MainActivity.java:449) 12-20 18:50:09.405: E/AndroidRuntime(17117): at android.os.Handler.handleCallback(Handler.java:733) 12-20 18:50:09.405: E/AndroidRuntime(17117): at android.os.Handler.dispatchMessage(Handler.java:95) 12-20 18:50:09.405: E/AndroidRuntime(17117): at android.os.Looper.loop(Looper.java:136) 12-20 18:50:09.405: E/AndroidRuntime(17117): at android.app.ActivityThread.main(ActivityThread.java:5081) 12-20 18:50:09.405: E/AndroidRuntime(17117): at java.lang.reflect.Method.invokeNative(Native Method) 12-20 18:50:09.405: E/AndroidRuntime(17117): at java.lang.reflect.Method.invoke(Method.java:515) 12-20 18:50:09.405: E/AndroidRuntime(17117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) 12-20 18:50:09.405: E/AndroidRuntime(17117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-20 18:50:09.405: E/AndroidRuntime(17117): at dalvik.system.NativeStart.main(Native Method) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload