创建Android小部件时出现java空指针异常
我正在为我的Android手机编写一段代码,它将使用其创建Android小部件时出现java空指针异常,java,android,Java,Android,我正在为我的Android手机编写一段代码,它将使用其个人扫描在该区域创建一个WifiDevices列表 创建列表时,我得到了java.lang.NullPointerException 我的代码如下: public static synchronized void addDevice(DeviceInformation device, View v, Context con, boolean bool, int type) throws IOException { Log.v("add
个人扫描
在该区域创建一个WifiDevices
列表
创建列表时,我得到了java.lang.NullPointerException
我的代码如下:
public static synchronized void addDevice(DeviceInformation device, View v, Context con, boolean bool, int type) throws IOException {
Log.v("addDevice", "Called");
if (bool) {
TableLayout tb = (TableLayout) v.findViewById(R.id.DeviceList);
LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(con);
TextView tv = new TextView(con);
System.out.println(v.toString());
tv.setLayoutParams(layout);
tr.setLayoutParams(layout);
String message;
Log.v("addDevice","Device Timeout");
switch(type) {
case 1:
computerEnd = true;
break;
case 2:
raspberryEnd = true;
break;
case 3:
flyportEnd = true;
break;
}
if (computerEnd && raspberryEnd && flyportEnd) {
if (rowCounter > 0) {
message = "No More Devices";
} else {
message = "No Devices Found";
}
tv.setText(message);
tv.setTextColor(Color.WHITE);
if (rowCounter % 2 == 0) {
tr.setBackgroundColor(Color.DKGRAY);
} else {
tr.setBackgroundColor(Color.GRAY);
}
tv.setVisibility(1);
tr.addView(tv);
tb.addView(tr); //This is line number 131
}
} else {
TableLayout tb = (TableLayout) v.findViewById(R.id.DeviceList);
LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(con);
TextView tv = new TextView(con);
tv.setLayoutParams(layout);
tr.setLayoutParams(layout);
Log.v("addDevice", "Received");
String textToDisplay = device.getDeviceTypeString() + "\n" + device.getIPAddress(); //Write the text to display
tv.setText(textToDisplay);
tv.setTextColor(Color.WHITE);
Drawable img;
if (device.getDeviceType() == 1) {
img = con.getResources().getDrawable(R.drawable.pc);
} else if (device.getDeviceType() == 2) {
img = con.getResources().getDrawable(R.drawable.raspberry);
} else {
img = con.getResources().getDrawable(R.drawable.flyport);
}
img.setBounds(0,0,70,45);
tv.setCompoundDrawables(null, null, img, null);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//empty
}
});
if (rowCounter % 2 == 0) {
tr.setBackgroundColor(Color.DKGRAY);
} else {
tr.setBackgroundColor(Color.GRAY);
}
rowCounter++;
Log.v("Result", "Device Added");
}
}
我的Logcat错误:
05-11 05:25:07.500: E/AndroidRuntime(30710): FATAL EXCEPTION: Thread-20873
05-11 05:25:07.500: E/AndroidRuntime(30710): java.lang.NullPointerException
05-11 05:25:07.500: E/AndroidRuntime(30710): at com.example.devicecontrolpanel.DeviceManagerWindow.addDevice(DeviceManagerWindow.java:131)
05-11 05:25:07.500: E/AndroidRuntime(30710): at com.connection.NetworkScanListenerRaspberry.run(NetworkScanListenerRaspberry.java:62)
05-11 05:25:07.500: E/AndroidRuntime(30710): at java.lang.Thread.run(Thread.java:864)
这就是达到此特定代码的方式:
此代码调用线程
,该线程反过来调用此方法
public void searchDevice(View view) throws IOException, InterruptedException
{
try
{
Thread ComputerListener = new Thread(new NetworkScanListenerComputer(getApplicationContext(),view));
ComputerListener.start();
}
catch(Exception e)
{
Log.v("Exception:","Exception from SearchDevice Method"+e.toString());
}
}
这是线程的代码:
package com.connection;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.example.devicecontrolpanel.DeviceManagerWindow;
public class NetworkScanListenerComputer implements Runnable
{
View thisView;
Context thisContext;
MulticastSocket socket = null;
DatagramPacket inPacket = null;
byte[] inBuf;
public NetworkScanListenerComputer(Context con, View v)
{
thisView = v;
thisContext = con;
try
{
socket = new MulticastSocket(WifiConstants.COMPUTER_RECV_PORT);
socket.joinGroup(InetAddress.getByName(WifiConstants.COMPUTER_NETWORK_ADDR));
inBuf = new byte[1024];
inPacket = new DatagramPacket(inBuf, inBuf.length);
socket.setSoTimeout(1*60*1000);
}
catch(Exception ioe)
{
Log.v("Exeception:","Computer Listener Exception"+ioe);
}
}
@Override
public void run()
{
try
{
while(true)
{
System.out.println("Listening...");
socket.receive(inPacket);
System.out.println("Received");
String msg = new String(inBuf, 0, inPacket.getLength());
DeviceInformation device = new DeviceInformation(1, msg, inPacket.getAddress().toString());
DeviceManagerWindow.addDevice(device, thisView, thisContext, false, 1);
Log.v("Received:","Received Computer From :" + inPacket.getAddress() + " Msg : " + msg);
//System.out.write(inPacket.getData(),0,inPacket.getLength());
System.out.println();
Thread.sleep(2000);
}
}
catch(Exception e)
{
Log.v("Exception:","During Receiving Computer: "+e.toString());
try
{
DeviceManagerWindow.addDevice(null, thisView, thisContext, true, 1);
}
catch (IOException e1)
{
Log.v("Exception:", "Computer End Error: " +e1);
}
}
finally
{
socket.close();
}
}
}
我正在传递应用程序的视图
和上下文
,它再次被重定向到静态方法
,因为静态方法无法使用它们
我解决了这个问题。。。它在说只有创建视图层次结构的原始线程才能接触其视图。
这就是创建视图的线程(
DeviceManagerWindow.java
)只能访问R.id.deviceList
。那么,如果我想访问它,我应该怎么做 那么,您应该做的第一件事就是调试,以准确地获取空指针异常的位置。但是,如果仔细查看代码,则有两种可能出现空指针异常,第一种可能是:
TableLayout tb = (TableLayout) v.findViewById(R.id.DeviceList);
因为视图v未找到id为“DeviceList”的任何视图
或在此:
if(device.getDeviceType()==1)
因为设备正在方法“null”中传递。
那么,看看这两个。
祝您好运问题似乎是您正试图从后台
线程
更新UI
。您可以使用runOnUiThread()
,但我认为更简单的方法是将线程
变成异步任务
。然后您可以在doinbackground()
中完成所有网络工作,并在onPostExecute()中更新UI
如果这是一个单独的文件,而不是内部类,那么您可以将上下文
传递给构造函数,以便更新UI
。或者,您可以返回一个值,该值告诉调用的活动
。我不确定,但看起来你可能会一次叫这个。如果是这样的话,我建议在UI
正在做一些事情时调用一个任务来获取后台的所有设备,并从那里将它们添加到列表中。另一件事是,您的线程中可能有一个无限循环,其中为true
。如果是这种情况,我会将其更改为侦听值更改的内容。希望这有帮助第131行是哪一行?这将大大有助于确定此处的null
。null指针异常非常常见。检查您的设备管理器窗口实例是否为空。我认为你发布的代码不是你例外的原因。告诉我们有关您的代码的更多信息将很有帮助。tb
为空,因为findViewById()
未找到此视图。请检查DeviceList
是否位于您通过+1的视图中。不确定为什么会获得否决票。相关代码,logcat,指出了哪一行有错误,描述了OP发生了什么…一个好问题的所有要素有很多地方可以产生NPE
,但是只有一个地方可能是OP发布的logcat的罪魁祸首,这更接近但是设备。getDeviceType()
不能成为问题,因为它位于与发生NPE
的块不同的块中。根据logcat,错误发生在if
块中,但该代码在else
块中我发现了问题。。。它在说只有创建视图层次结构的原始线程才能接触其视图。
即创建视图的线程(DeviceManagerWindow.java
)只能访问R.id.deviceList
。那么,如果我想访问它,我应该怎么做。?