Fragment cannot be cast to android.content.Context - java

I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.

Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);

Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);

Related

Passing Integer Values Between Activities - Android

Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));

Setting up an avatar on android app

I'm wanting to let the user create an avatar (profile picture if you like) when they set up their user info. I've created a method for a single click/touch which would ask the user to take a picture and one for a long click which would ask the user to choose a picture from their gallery.
Below are my methods from the class file:
public void onLaunchCamera(View v) {
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
}
And below is the XML associated with the ImageButton:
<ImageButton
android:id="#+id/ImageButton_Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="#dimen/avatar_size"
android:minHeight="#dimen/avatar_size"
android:onClick="onLaunchCamera"
android:scaleType="fitXY"
android:src="#drawable/avatar"></ImageButton>
All it does is crash when I click on the ImageButton and I have no idea why. Any ideas?
Thanks
EDIT: Adding the logcat below (Sorry about the formatting. Couldn't work out how to get it all sorted properly:
[ 04-12 18:32:50.989 5901: 5901 D/ ]
HostConnection::get() New Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300 Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab, PID: 5901 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 a 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: java.lang.NullPointerException
at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
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) 
It looks like you might not have defined avatarButton, if you follow the Caused By path on the LogCat you see the bottom one is a NullPointerException.
Since I can't see the line numbers, the issue is happening on line 201--the only obvious null pointer I see in your code is avatarButton.
Based on what you want to do, you'll want to go about this a bit differently.
Remove the android:onClick="onLaunchCamera" from the XML.
in your onCreate() after you set the content view add the following:
View avatarButton = findViewById(R.id.ImageButton_Avatar);
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
This allows you to set both a click and a longClick listener with more control. The way you had it, you were never really defining the onClick or onLongClick until you clicked on them the first time.

Android NumberPicker NullPointer why?

i am try to implement a numberPicker to select minute values.
But i am getting a NullPointer Exception at this line:
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
Following Code:
public class MainActivity extends Activity {
NumberPicker minutePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Auswahl Minuten zum starten / Stoppen aller
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
minutePicker.setMaxValue(30);
minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
abschaltzeit = minutePicker.getValue();
}
});
minutePicker.setValue(0);
minutePicker.setWrapSelectorWheel(false);
}
}
XML:
<NumberPicker
android:id="#+id/minuten_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="6"
android:layout_column="0"
android:paddingLeft="20dp" />
Log:
09-25 11:00:09.749 10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.carsten.awesome.app, PID: 10687
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.
Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.
If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()
If you choose the inflation way, make sure you have the view in your XML layout file.
I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.
I apologize.
I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.
Sorry but i am new with the Fragement Konzept.
Thanks a lot for your help !

Null Pointer Exception in Bitmap.decodeResources() from xml resource

In my current android project I create two bitmaps from xml resources. Every time I build my project using the xml bitmaps as the resource for Bitmap.decodeResources() I get a null pointer exception when I try to access the bitmaps. But when I switch to using the straight .png the error goes away.
Why is decodeResources() returning null?
code where the bitmaps are created:
public OvertButton(float x, float y, float width, float height, int pressedId, int unPressedId, Resources res, boolean visible) {
\\these are null if xml bitmap is passed
pressed = BitmapFactory.decodeResource(res, pressedId);
unPressed = BitmapFactory.decodeResource(res, unPressedId);
this.visible = visible;
relocate(x, y);
resize(width, height); //throws NPE when I createScaledBitmap()
ClickableManager.registerClickable(this);
}
resource file
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/play_button_pressed"
android:filter="false"
android:antialias="false"
android:dither="false"/>
and the resize method
#Override
public void resize(float width, float height) {
box.set(box.left, box.top, box.left + width, box.top + height);
Log.d("resize",String.valueOf(height));
pressed = Bitmap.createScaledBitmap(pressed, (int)width, (int)height, true);
unPressed = Bitmap.createScaledBitmap(unPressed, (int)width, (int)height, true);
}
And the logcat
04-16 09:45:06.341 11824-11824/com.handmade.eed D/dalvikvm﹕ Late-enabling CheckJNI
04-16 09:45:06.450 11824-11824/com.handmade.eed D/MenuActivity﹕ 2130837594$$$2130837596
04-16 09:45:06.451 11824-11824/com.handmade.eed D/skia﹕ --- SkImageDecoder::Factory returned null
04-16 09:45:06.451 11824-11824/com.handmade.eed D/OvertButton﹕ true2130837591
04-16 09:45:06.457 11824-11824/com.handmade.eed D/OvertButton﹕ false2130837595
04-16 09:45:06.457 11824-11824/com.handmade.eed D/resize﹕ 252.0
04-16 09:45:06.458 11824-11824/com.handmade.eed D/AndroidRuntime﹕ Shutting down VM
04-16 09:45:06.459 11824-11824/com.handmade.eed W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41610d40)
04-16 09:45:06.461 11824-11824/com.handmade.eed E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.handmade.eed, PID: 11824
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.handmade.eed/com.handmade.eed.MenuActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at com.handmade.overt.visible.OvertButton.resize(OvertButton.java:61)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:29)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:34)
at com.handmade.eed.MenuActivity.onCreate(MenuActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
04-16 09:45:09.872 11824-11824/com.handmade.eed I/Process﹕ Sending signal. PID: 11824 SIG: 9
This is a case of me being dumb but here is the answer for posterity.
Bitmap is not the same as BitmapDrawable
From the Drawable Resources link above:
COMPILED RESOURCE DATATYPE:
Resource pointer to a BitmapDrawable.
Thus I had to change this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed_res); //this is the id for the xml referring to the png
to either this:
Drawable pressed = res.getDrawable(R.drawable.play_button_pressed_res);
or this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed); //this is the id for the png
I decided to use bitmap because, as I understand, it is faster.

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