-5

I must make an android app, that can controll a playcar! I have a seekbar and with this can i controll how fast it is! And every time, it change the app should send the data on a website of a friend! My Problem is that, if i try to test the app, the app crashs! It shows no fails it simply crash! Here is the code:

activity_main.xml: '''

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/seekbar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginBottom="380dp"
        android:max="10"
        android:rotation="270"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textseek"
        android:layout_width="100sp"
        android:layout_height="36sp"
        android:layout_marginStart="62sp"
        android:layout_marginLeft="62sp"
        android:layout_marginTop="120sp"
        android:text="@string/_1"
        android:textColor="#00FF00"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="62dp"
        android:layout_height="57dp"
        android:layout_marginStart="62dp"
        android:layout_marginLeft="62dp"
        android:layout_marginBottom="163dp"
        android:contentDescription="@string/leftround"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/left" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="62dp"
        android:layout_height="57dp"
        android:layout_marginEnd="95dp"
        android:layout_marginRight="95dp"
        android:layout_marginBottom="163dp"
        android:contentDescription="@string/rightround"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/right" />


</androidx.constraintlayout.widget.ConstraintLayout>
'''
'''
MainActivity.java:

...
package com.example.unwichtig;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.RemoteActionCompatParcelizer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class MainActivity extends AppCompatActivity {

    public TextView text;
    public ImageButton left;
    public ImageButton right;
    public int prozess = 0;
    public int AppRichtungL;
    public int AppRichtungR;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        SeekBar sSeekBar = findViewById(R.id.seekbar);
        text = findViewById(R.id.textseek);
        left = (ImageButton) findViewById(R.id.imageButton1);
        right = (ImageButton) findViewById(R.id.imageButton2);

sSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

            @Override
            public void onProgressChanged(SeekBar sSeekBar, final int progress,
                                          boolean fromUser) {
               // text.setText(String.valueOf(progress*51));
            prozess = Integer.valueOf((progress*51) - 255);
               text.setText(String.valueOf(prozess));

               //stellt je nach Stellung der SeekBar die Richtung ein
                VorRueckwaerts();


               left.setOnTouchListener(new View.OnTouchListener() {
                   @Override
                   public boolean onTouch(View v, MotionEvent event) {
                      switch(event.getAction()){
                          //Wenn Button gedrückt:
                          case MotionEvent.ACTION_DOWN: {
                              prozess = 51;
                              //Microcontroller();
                              //postData();
                                break;
                          }
                          //Wenn Button losgelassen wird:
                          case MotionEvent.ACTION_UP: {
                              prozess = (progress*51)-255;
                              VorRueckwaerts();
                                break;
                          }

                      }
                       return false;
                   }

               });
                right.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()){
                            //Wenn Button gedrückt:
                            case MotionEvent.ACTION_DOWN: {
                                //Microcontroller();
                                //postData();
                                prozess = 51;
                                AppRichtungL = 0;
                                AppRichtungR = 1;
                                break;
                            }
                            //Wenn Button losgelassen wird:
                            case MotionEvent.ACTION_UP: {
                              prozess = (progress*51)-255;
                                VorRueckwaerts();
                                break;
                            }

                        }
                        return false;
                    }

                });

            }

            @Override
            public void onStartTrackingTouch(SeekBar sSeekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar sSeekBar) {
            }
        });

    }

    public void VorRueckwaerts(){
        if(prozess < 0)
        {
            AppRichtungL = 0;
            AppRichtungR = 0;
        }

        if(prozess > 0)
        {
            AppRichtungL = 1;
            AppRichtungR = 1;
        }
        //Microcontroller();
        //postData();
        send();
    }

    public void send()
    {
        // get the message from the message text box

        // make sure the fields are not empty
        String msg = text.getText().toString();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://schueler.bulme.at//~maximilian.rupp/spycar");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                nameValuePairs.add(new BasicNameValuePair("message", msg));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }


    }
}

'''

djleop
  • 677
  • 4
  • 17
  • 2
    Where is the crash log? – tomerpacific Apr 23 '20 at 11:28
  • 1
    Hi dominator x, please provide us the crash log, visible in the logcat pane of Android Studio. If you don't see your crash log, then you maybe looking at the wrong place. I can see that you catch errors and do nothing about it : // TODO Auto-generated catch block You should at least log the error in order to see something. The app could crash maybe cause you don't request permissions, you should take a look at the basics of Android development and Internet access. https://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application#2378619 – djleop Apr 23 '20 at 11:56
  • This is the Problem, it crashs and give my no Crash log – dominator x Apr 24 '20 at 12:10
  • I've never heard of an app crashing with no crash log. Perhaps it could be exiting without a crash, but there should be _something_ in the logs. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M May 09 '20 at 03:23
  • Ok thanks for the links, i will lock and hope it helps – dominator x May 10 '20 at 12:46

1 Answers1

1

You are trying to send your HTTPS request from the UI (main) thread of your application.

This is not allowed in Android and this throws an exception.

You should use an AsyncTask to run the http code.

AsyncTask is about to be deprecated in the next version of Android, however it is a simpler solution for you case (at least during testing) then the Google recommended alternative which is coroutines.

Lev M.
  • 4,162
  • 1
  • 6
  • 21
  • Thanks for the answer, do you know a good tutorial Site, what shows my, how i can do this, i am real new in android studios and dont know what functions i need and so on – dominator x Apr 24 '20 at 12:11