我编写了一个使用runOnUiThread的线程,大约每500毫秒更新一次listview。该线程写在OnCreate()之外,但是从OnCreate调用。一切编译都很好,但我似乎得到一个错误,告诉我我的适配器是null,即使我使用相同的适配器来“初始化”OnCreate中的列表视图。我已经发布了我的代码和我的日志。我遇到的主要错误java.lang.NullPointerException指的是MainActivity的第454行,在本例中是adapter.notifyDataSetChanged();
。任何帮助将不胜感激。
MainActivity
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 < 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;
}
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();
}
}
日志
12-21 14:39:02.109: D/AndroidRuntime(32704): Shutting down VM
12-21 14:39:02.109: W/dalvikvm(32704): threadid=1: thread exiting with uncaught exception (group=0x4198ac68)
12-21 14:39:02.109: E/AndroidRuntime(32704): FATAL EXCEPTION: main
12-21 14:39:02.109: E/AndroidRuntime(32704): Process: com.byrdonatwigge.sense, PID: 32704
12-21 14:39:02.109: E/AndroidRuntime(32704): java.lang.NullPointerException
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.byrdonatwigge.sense.MainActivity$2$1.run(MainActivity.java:454)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Handler.handleCallback(Handler.java:733)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Handler.dispatchMessage(Handler.java:95)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Looper.loop(Looper.java:136)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.app.ActivityThread.main(ActivityThread.java:5081)
12-21 14:39:02.109: E/AndroidRuntime(32704): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 14:39:02.109: E/AndroidRuntime(32704): at java.lang.reflect.Method.invoke(Method.java:515)
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 14:39:02.109: E/AndroidRuntime(32704): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
@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 < ids.length; i++)
{ids[i] = Integer.toString(i+1);}
// change following line:
this.adapter = new ItemAdapter(this,R.layout.row, ids);
答案 1 :(得分:1)
您正在adapter
中创建一个本地变量onCreate()
,并为其分配一个新的ItemAdapter
。
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
这称为variable shadowing;该变量与您尝试在adapter
中使用的实例字段GUI_Update()
完全无关。这就是为什么它在那里为空,并抛出NPE。
您的IDE实际上应该指出这一点。