NullPointerException on OutputStream using BluetoothSocket? - java

I'm trying to send data to an HC-05 bluetooth module but am not sure what I'm doing wrong. My main code where the problem lies is below.
I'm trying to just grab some specific text and send it off in bytes.
I've already paired my module before starting the app, and loop through all the paired devices using getBondedDevices() on my BluetoothAdapter and can confirm it is paired.
public void send(View view) {
try {
Log.d("TAG", "Sending!!!");
OutputStream os = mConnectionSocket.getOutputStream();
Log.d("TAG", "Text is: " + mSendText.getText().toString().getBytes());
// This lines under produces the error
os.write(mSendText.getText().toString().getBytes());
os.close();
Log.d("TAG", "Written...");
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the code I use to get a BluetoothSocket after confirming the device is connected. mModule is the BluetoothDevice object I want to send data to.
if(isAlreadyConnected) {
try {
mConnectionSocket = mModule.createRfcommSocketToServiceRecord(mUUID);
new Thread(new Runnable() {
#Override
public void run() {
try {
mConnectionSocket.connect();
} catch (IOException e ) {e.printStackTrace();}
}
});
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.d("TAG", "Not connected!");
}
This is the stack trace of the error.
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3969)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19421)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5479)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:510)
at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
at java.io.OutputStream.write(OutputStream.java:82)
at **com.test.arduinobluetoothdatasender.MainActivity.send(MainActivity.java:133)**
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5479)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
The error goes away when I comment out this:
os.write(mSendText.getText().toString().getBytes());
Not really sure what I am doing wrong, but it is probably something obvious.

Related

networkInfo.isConnected() error

I'm receiving an error when executing this code.
When connected to wifi, the function should display MAC address, otherwise "---"
private void DisplayWifiStatus() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conMgr.getActiveNetworkInfo();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
if (networkInfo.isConnected()) {
textMac.setText(info.getMacAddress());
}else {
textMac.setText("-----");
}
}
Logcat/Stacktrace:
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> Normal User
03-11 21:32:07.532 3348-3348/com.light87sakurahimegmail.wifiscanner E/dalvikvm﹕ >>>>> com.light87sakurahimegmail.wifiscanner [ userId:0 | appId:10337 ]
03-11 21:32:07.782 3348-3348/com.light87sakurahimegmail.wifiscanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.light87sakurahimegmail.wifiscanner, PID: 3348
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.light87sakurahimegmail.wifiscanner/com.light87sakurahimegmail.wifiscanner.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
at android.app.ActivityThread.access$800(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.light87sakurahimegmail.wifiscanner.MainActivity.DisplayWifiStatus(MainActivity.java:79)
at com.light87sakurahimegmail.wifiscanner.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access$800(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Android Application java lang exception

I have created an android application using Android Studio IDE while clicking the button in my app it does'nt navigate it to the another activity instead its throwing up the exception
Here is My Logcat:
02-10 11:33:56.030 7404-7404/com.example.mcs.oodoeg E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:4275)
at android.view.View$PerformClick.run(View.java:17434)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ExceptionInInitializerError
at org.apache.xmlrpc.common.TypeFactoryImpl.<clinit>(TypeFactoryImpl.java:88)
            at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
            at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
            at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
            at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
            at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
            at android.view.View.performClick(View.java:4275)
            at android.view.View$PerformClick.run(View.java:17434)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: org.apache.ws.commons.serialize.DOMSerializer
at org.apache.xmlrpc.serializer.NodeSerializer.<clinit>(NodeSerializer.java:30)
            at org.apache.xmlrpc.common.TypeFactoryImpl.<clinit>(TypeFactoryImpl.java:88)
            at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
            at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
            at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
            at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
            at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
            at android.view.View.performClick(View.java:4275)
            at android.view.View$PerformClick.run(View.java:17434)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
This is my Main Activity class i have created two buttons here called register and signin:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intObj = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intObj);
}
});
Button btn2;
btn2=(Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent= new Intent(MainActivity.this,Sigin.class);
startActivity(intent);
}
} );
}
When the register button is clicked this Main Activity2 should be called:
public class MainActivity2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
}
When sigin button is clicked this sigin class should be called:
public class Sigin extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sigin);
}
}
I think you are missing ws-commons-util-1.0.2.jar from $EXIST_HOME/lib/core.
You have to make sure you have included at least all of the libraries from $EXIST_HOME/lib/core.

Problems in Parcelable Class using a List of Custom Objects (Defined in an Inner Class)

I'm trying to make my class Parcelable, but I keep getting these errors:
08-28 07:42:55.598 7810-7810/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan)
at android.os.Parcel.writeSerializable(Parcel.java:1279)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeList(Parcel.java:622)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6850)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
at android.app.Activity.startActivityForResult(Activity.java:3388)
at android.app.Activity.startActivityForResult(Activity.java:3349)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeList(Parcel.java:622)
            at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
            at android.os.Parcel.writeParcelable(Parcel.java:1254)
            at android.os.Parcel.writeValue(Parcel.java:1173)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1627)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.content.Intent.writeToParcel(Intent.java:6850)
            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
            at android.app.Activity.startActivityForResult(Activity.java:3388)
            at android.app.Activity.startActivityForResult(Activity.java:3349)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
            at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
Here's the structure of my class; it has an inner class. I don't think it's ideal to put my entire code. It's just too long.
public class CropRotationPlan implements Parcelable {
String name; // Name of plan
HashMap<String, List<HashMap<String, String>>> listOfFamilies; // HashMap containing Plant Family Names as keys and a List of plants
List<SubPlan> listOfSubPlans;
public CropRotationPlan() {
name = null;
listOfFamilies = new HashMap<>();
listOfSubPlans = new ArrayList<>();
}
...
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeSerializable(this.listOfFamilies);
dest.writeList(this.listOfSubPlans);
}
protected CropRotationPlan(Parcel in) {
this.name = in.readString();
this.listOfFamilies = (HashMap<String, List<HashMap<String, String>>>) in.readSerializable();
this.listOfSubPlans = new ArrayList<SubPlan>();
in.readList(this.listOfSubPlans, List.class.getClassLoader());
}
public static final Parcelable.Creator<CropRotationPlan> CREATOR = new Parcelable.Creator<CropRotationPlan>() {
public CropRotationPlan createFromParcel(Parcel source) {
return new CropRotationPlan(source);
}
public CropRotationPlan[] newArray(int size) {
return new CropRotationPlan[size];
}
};
public class SubPlan implements Serializable {
int year;
HashMap<String, Integer> orderOfFamilies;
...
}
}
At this point, I seriously have no idea what to do.
CropRotationPlan.java:248 is dest.writeList(this.listOfSubPlans); in writeToParcel()
Also, in another class, I have this method:
public void btnViewPlan(View view) {
// We'll be sending our cropRotationPlan object to next activity (PlanActivity)
// And we'll be expecting a result (since we'll allow the user to manipulate the object there)
Intent i = new Intent(CropRotationPlannerActivity.this, PlanActivity.class);
i.putExtra("cropRotationPlan", cropRotationPlan);
startActivityForResult(i, SET_PLAN_REQUEST);
}
CropRotationPlannerActivity.java:197 is: startActivityForResult(i, SET_PLAN_REQUEST);
I've tried placing my inner class into a separate class, and then had it to implement Serializable OR Parcelable (just one of 'em) but I just keep getting errors. All relating to my SubPlan class; like problems with marshing it and stuff. Along with using TypedList instead somewhere in the code.
Help would be appreciated. Thanks!
I just don't know what to do anymore.
PS: I'm sorry if I sound so incoherent right now. I might edit this post once I recover my sanity.
CODE FOR SubPlan CLASS
public class SubPlan implements Serializable {
int year;
HashMap<String, Integer> orderOfFamilies;
public SubPlan(int year) {
orderOfFamilies = new HashMap<>();
this.year = year;
int temp = -1;
for (String key : listOfFamilies.keySet()) {
orderOfFamilies.put(key, ++temp);
}
}
public SubPlan(int year, HashMap<String, Integer> orderOfFamilies) {
this.year = year;
this.orderOfFamilies = orderOfFamilies;
}
public int getYear() {
return year;
}
public String getName() {
return "Year " + year;
}
public String getKey(int index) {
for (String key : orderOfFamilies.keySet()) {
if (orderOfFamilies.get(key).equals(index))
return key;
}
return null;
}
public int getLocationOf(String family) {
return orderOfFamilies.get(family);
}
public void setOrderOfFamily(String family, int index) {
if (orderOfFamilies.containsKey(family))
orderOfFamilies.put(family, index);
}
public void removeFamily(String family) {
if (orderOfFamilies.containsKey(family)) {
orderOfFamilies.remove(family);
}
}
public void rotateFamilies() {
for (int i = 0; i < getNumberOfFamilies(); i++) {
String family = getKey(i);
// if not last family on list
if (orderOfFamilies.get(family) != getNumberOfFamilies() - 1)
setOrderOfFamily(family, i + 1);
else
setOrderOfFamily(family, 0);
}
}
}
Additionally, getNumberOfFamilies() is a method belonging to the parent class, with the following definition:
public int getNumberOfFamilies() {
return listOfFamilies.size();
}
I have initially not let SubPlan implement Serializable and these are the exceptions I get:
08-28 08:31:07.809 8741-8741/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcel: unable to marshal value thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan#4191f010
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:249)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6850)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
at android.app.Activity.startActivityForResult(Activity.java:3388)
at android.app.Activity.startActivityForResult(Activity.java:3349)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
08-28 08:31:09.543 531-1643/? E/InputDispatcher﹕ channel '41d34990 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-28 08:31:09.543 531-1643/? E/InputDispatcher﹕ channel '4281f970 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.CropRotationPlannerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
Additionally, I have tried implementing Parcelable on SubPlan and made it have its own class:
08-28 08:54:59.291 9245-9245/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
            at android.view.View.performClick(View.java:4222)
            at android.view.View$PerformClick.run(View.java:17620)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.StackOverflowError
at java.io.SerializationHandleMap.get(SerializationHandleMap.java:63)
at java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:425)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1566)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.HashMap.writeObject(HashMap.java:1015)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.ArrayList.writeObject(ArrayList.java:644)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.HashMap.writeObject(HashMap.java:1016)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:185)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.write
The root cause is listed at the bottom, and the error says it all:
java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan
The class CropRotationPlan must be Serializable.
Update
Because SubPlan is not static, serializing SubPlan also means serializing the outer class CropRotationPlan. Since only the SubPlan constructor seems to use the outer class, change the class to static and send listOfFamilies in as a parameter on the constructor.

Create Custom View and Add to XML

I have created a custom View which draws a line:
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 100, 100, paint);
}
}
I have a MainActivity class and activity_main layout where my layout contains an image and several buttons. I want to put the custom view that I created to my XML file(activity_main) where a line would be drawn on top of the image.
I tried adding it:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
//imageview and buttons here
<com.myapp.alphasickbro.myapplication2.DrawView
android:id="#+id/paintView"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</RelativeLayout>
Here is my MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
But when I run it, the app unfortunately stops. Did I miss something? What lines of code should I add?
Any answer would be appreciated
EDIT:
04-18 15:24:47.517 26732-26732/com.myapp.alphasickbro.myapplication2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.alphasickbro.myapplication2, PID: 26732
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.alphasickbro.myapplication2/com.myapp.alphasickbro.myapplication2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.myapp.alphasickbro.myapplication2.DrawView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.myapp.alphasickbro.myapplication2.DrawView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:2002)
at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:2002)
            at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5411)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myapp.alphasickbro.myapplication2.DrawView.<init>(DrawView.java:27)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:2002)
            at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5411)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
I think you need to add the missing constructors. Clean your project.
public class DrawView extends View {
Paint paint;
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
}
}

Android socket client doesn't work

I try to create a socket client connection to a python server like this, but it throwss me an error. This is triggered via a button, which works. The server is running on a RPi, but that's not the problem since a Python client works on my PC.
try {
Socket s = new Socket("192.168.178.43", 24069);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
} catch (UnknownHostException e) {
System.err.println("Unknown Host.");
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection.");
System.err.println(e);
// System.exit(1);
}
Stacktrace:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:567)
at java.net.Socket.tryAllAddresses(Socket.java:128)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at com.example.app.MainActivity.sendMessage(MainActivity.java:65)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
you can't do any long time process on the Main UI thread the reason for that to avoid the crash of your App if your operation failed
try to create new thread and do your work inside of it and if u want to pass any result to the UI u cant use
View.Post()
or
context.runOnUiThread()
this is an example of what you can do or you can use asynctask instead
new Thread(new Runnable() {
public void run() {
try {
Socket s = new Socket("192.168.178.43", 24069);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
} catch (UnknownHostException e) {
System.err.println("Unknown Host.");
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection.");
System.err.println(e);
// System.exit(1);
}
}
}).start();

Categories