我正在开发一个基于套接字的应用程序。但问题是我的应用程序在模拟器中正常工作但是当我在真实设备中测试时它崩溃了很多次。
这是我的 Logcat :
01-21 16:24:38.667: E/AndroidRuntime(3525): FATAL EXCEPTION: main
01-21 16:24:38.667: E/AndroidRuntime(3525): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.inextrix.automation/org.inextrix.automation.MainActivity}: java.lang.NullPointerException
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.os.Looper.loop(Looper.java:137)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-21 16:24:38.667: E/AndroidRuntime(3525): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:24:38.667: E/AndroidRuntime(3525): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 16:24:38.667: E/AndroidRuntime(3525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-21 16:24:38.667: E/AndroidRuntime(3525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-21 16:24:38.667: E/AndroidRuntime(3525): at dalvik.system.NativeStart.main(Native Method)
01-21 16:24:38.667: E/AndroidRuntime(3525): Caused by: java.lang.NullPointerException
01-21 16:24:38.667: E/AndroidRuntime(3525): at org.inextrix.automation.MainActivity$1.updateStatus(MainActivity.java:272)
01-21 16:24:38.667: E/AndroidRuntime(3525): at org.inextrix.automation.MainActivity$1.run(MainActivity.java:250)
01-21 16:24:38.667: E/AndroidRuntime(3525): at org.inextrix.automation.MainActivity.startRepeatingTask(MainActivity.java:485)
01-21 16:24:38.667: E/AndroidRuntime(3525): at org.inextrix.automation.MainActivity.onCreate(MainActivity.java:205)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.Activity.performCreate(Activity.java:4492)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-21 16:24:38.667: E/AndroidRuntime(3525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-21 16:24:38.667: E/AndroidRuntime(3525): ... 11 more
这是我的MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getSharedSettings();
fanDimmer1=(ToggleButton)findViewById(R.id.button_fan1);
fanDimmer2=(ToggleButton)findViewById(R.id.button_fan2);
dimmerLight1=(ToggleButton)findViewById(R.id.button_light1);
dimmerLight2=(ToggleButton)findViewById(R.id.button_light2);
fanDimmer1.setOnClickListener(this);
fanDimmer2.setOnClickListener(this);
dimmerLight1.setOnClickListener(this);
dimmerLight2.setOnClickListener(this);
if(ip.equals("") || port.equals(""))
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("Warning !")
.setMessage("Please set IP and PORT first")
.setIcon(android.R.drawable.ic_dialog_alert)
.setNeutralButton("ok", null)
.show();
}
else
{
new Thread(new Runnable()
{
@Override
public void run()
{
Log.v(TAG, "openconnection");
openConnection();
}
}).start();
m_handler = new Handler();
startRepeatingTask();
}
}
public void openConnection()
{
// TODO Auto-generated method stub
try
{
s = new Socket(ip, Integer.parseInt(port));
i = s.getInputStream();
iD = new DataInputStream(i);
o = s.getOutputStream();
oD = new DataOutputStream(o);
Log.v(TAG, "openconnection 2");
}
catch (UnknownHostException e) {
// TODO: handle exception
Log.v("UnknowHostException :::::", "In Catch Block");
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
Log.v("IOException :::::", "In Catch Block");
e.printStackTrace();
}
}
Runnable m_statusChecker = new Runnable()
{
@Override
public void run()
{
if (count == 0) {
updateStatus();
count = 1;
} else {
updateStatus1();
count = 0;
}
m_handler.postDelayed(m_statusChecker,1000);
}
private void updateStatus()
{
// TODO Auto-generated method stub
Log.v("test", "1");
try {
byte[] data1 = new byte[1024], packet1 =
{
(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x06,
(byte) 0x01,(byte) 0x01,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x19
};
o.write(packet1); //line number 272
i.read(data1, 0, 1024);
byte_to_hex = ConversionMethods.bytesToHex(data1).substring(18, 26);
/*Log.d(TAG,"Original String ::: "+ byte_to_hex);*/
char[] arr = byte_to_hex.toCharArray();
for (int i = 0; i < arr.length - 1; i += 2)
{
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
swapped_result=new String(arr);
result = ConversionMethods.hexStringToNBitBinary(swapped_result, 32);
int counter = 0;
for( int i=0; i<result.length(); i++ )
{
if( result.charAt(i) == '1' )
{
counter++;
}
}
status=Integer.toString(counter);
txt_status.setText(status);
Log.v(TAG, "status is ::"+status);
char[] c=result.toCharArray();
int count=0;
for (int i=0;i<result.length();i++)
{
count++;
char j=c[i];
//Log.v(TAG, count+"::"+j);
if(count==1)
toggleButton=dimmerLight1;
else if(count==2)
toggleButton=fanDimmer2;
else if(count==3)
toggleButton=fanDimmer1;
else if(count==4)
Log.v(TAG, "Count 4 is 0");
if(j=='1')
toggleButton.setChecked(true);
else
toggleButton.setChecked(false);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void updateStatus1()
{
Log.v("test", "2");
try {
byte[] data1 = new byte[1024], packet1 =
{
(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x06,
(byte) 0x01,(byte) 0x03,(byte) 0x00,
(byte) 0x01,(byte) 0x00,(byte) 0x04
};
o.write(packet1);
i.read(data1, 0, 1024);
/*Log.v("::::TX::::", ConversionMethods.bytesToHex(data1).substring(0, 34));
Log.v("::::RX::::", ConversionMethods.bytesToHex(packet1));*/
d1=ConversionMethods.bytesToHex(data1).substring(20, 22);
d2=ConversionMethods.bytesToHex(data1).substring(24, 26);
d3=ConversionMethods.bytesToHex(data1).substring(28, 30);
d4=ConversionMethods.bytesToHex(data1).substring(32, 34);
Log.v(TAG, "D1="+d1);
Log.v(TAG, "D2="+d2);
Log.v(TAG, "D3="+d3);
Log.v(TAG, "D4="+d4);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
void startRepeatingTask() {
m_statusChecker.run();
}
void stopRepeatingTask() {
m_handler.removeCallbacks(m_statusChecker);
}
我被这个问题困住了,无法找到发生这种情况的原因。请给我一些想法,我该如何解决这个问题?
任何想法和帮助将不胜感激。
谢谢