lawang code

lawang Code


Tanyakan kode disini

Aplikasi buatan saya masih force close di device lain

ditanyakan oleh Yohanes Erwin 4 tahun yang lalu

Permisi, saya sedang membuat aplikasi berbasis android yang menggunakan layanan API Google Map. Ketika dijalankan di device xiaomi dan oppo terjadi force close saat memasuki activity yang terdapat Petanya.

ini potongan code saya dari activity yang dimaksud:

package com.example.javier.SIDeKaMobile;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import adapter.windowAdapter;
import services.WebDataService;
import services.WebService;
import utils.Constants;
import utils.Utils;

public class Activity_MapKecamatan extends AppCompatActivity implements WebDataService, GoogleMap.OnMapClickListener {

    private GoogleMap myMap;

    HashMap<String, Object> polygonList = new HashMap<>();
    ArrayList<Polygon> polygons = new ArrayList<>();

    List<String> colorArray = new ArrayList<>();

    private String jsonString = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(Build.VERSION.SDK_INT < 19){
            FrameLayout statusBar = (FrameLayout) findViewById(R.id.statusBar);
            ViewGroup.LayoutParams layoutParams = statusBar.getLayoutParams();
            layoutParams.height = 0;
        }
        setSupportActionBar(toolbar);

        //if(null != toolbar){
        toolbar.setNavigationIcon(R.mipmap.ic_ab_up_compat);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //}
        colorArray.add("#26a69a");
        colorArray.add("#004d40");
        colorArray.add("#afb42b");
        colorArray.add("#ff9800");
        colorArray.add("#ffff00");
        colorArray.add("#6d4c41");
        colorArray.add("#616161");
        colorArray.add("#212121");
        colorArray.add("#37474f");
        colorArray.add("#bf360c");
        System.out.println("Color = " + colorArray.get(Integer.parseInt(GenerateNum())));
        try{
            initializeMap();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void initializeMap() {
        if (myMap == null) {
            myMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();

            myMap.setOnMapClickListener(this);
            Intent i = getIntent();
            Bundle b = i.getExtras();
            final String id = b.getString("id_kabupaten");
            // call web services
            System.out.println("URL = " + Constants.GET_KECAMATAN);
            if(Utils.isNetworkAvailable(Activity_MapKecamatan.this)){
                new WebService(Activity_MapKecamatan.this, "").execute(Constants.GET_KECAMATAN+"/"+id);
            }else{
                Utils.showToast(Activity_MapKecamatan.this, "Periksa kembali sambungan internet anda!");
            }

            // check if map is created successfully or not
            if (myMap == null) {
                Toast.makeText(getApplicationContext(), "Tidak Dapat Membuat Peta!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void drawPolygon(String jsonString) throws JSONException {
        JSONObject jo = new JSONObject(jsonString);
        Iterator<String> keys = jo.keys();

        while (keys.hasNext()) {
            String key = keys.next();
            //String key2 = jo.names().getJSONObject(0).toString();
            System.out.println(key);
            JSONArray a = jo.getJSONArray(key);
            ArrayList<LatLng> p = new ArrayList<>();

            if (a.length() > 0) {
                PolygonOptions pOption = new PolygonOptions();
                pOption.strokeColor(Color.RED);
                pOption.strokeWidth(2);

                for (int j = 0; j < a.length(); j++) {
                    JSONArray b = a.getJSONArray(j);
                    pOption.fillColor(Color.parseColor(colorArray.get(Integer.parseInt(GenerateNum()))));
                    LatLng latlng = new LatLng(b.getDouble(0), b.getDouble(1));
                    pOption.add(latlng);
                    p.add(latlng);
                    /*if(j==0){
                     myMap.addMarker(new
                             MarkerOptions().position(latlng).title(b.getJSONObject(0).toString()));
                     }*/
                }
                polygons.add(myMap.addPolygon(pOption));
            }
            polygonList.put(key, p);
        }
    }

    @Override
    public void onMapClick(LatLng point) {
        Set<String> keys = polygonList.keySet();
        for (final String k : keys) {

            ArrayList<LatLng> vertices = (ArrayList<LatLng>) polygonList.get(k);
            if (isPointInPolygon(point, vertices)) {
                myMap.setInfoWindowAdapter(new windowAdapter(getLayoutInflater()));
                myMap.addMarker(new MarkerOptions().position(point).title(k).icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_icon))).showInfoWindow();
                myMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        marker.showInfoWindow();
                        //Snackbar.make(coordinatorLayoutView, "Anda Memilih Kecamatan " + marker.getTitle(), Snackbar.LENGTH_LONG).show();
                        return true;
                    }
                });
                myMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        Bundle b = new Bundle();
                        b.putString("Kecamatan", marker.getTitle());
                        Intent i = new Intent(getApplicationContext(), Activity_MapDesa.class);
                        i.putExtras(b);
                        startActivity(i);
                    }
                });
                /**/
                //Snackbar.make(coordinatorLayoutView, "Anda Memilih Kecamatan " + k, Snackbar.LENGTH_LONG).show();
                System.out.println("Click inside of polygon key =  " + k);
                break;
            } else {
                System.out.println("Click outside of polygon = " + point.toString());
                //m.setVisible(false);
            }
        }

    }

    private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
        int intersectCount = 0;
        for (int j = 0; j < vertices.size() - 1; j++) {
            if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
                intersectCount++;
            }
        }
        return ((intersectCount % 2) == 1); // odd = inside, even = outside;
    }

    private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {

        double aY = vertA.latitude;
        double bY = vertB.latitude;
        double aX = vertA.longitude;
        double bX = vertB.longitude;
        double pY = tap.latitude;
        double pX = tap.longitude;

        if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
                || (aX < pX && bX < pX)) {
            return false; // a and b can't both be above or below pt.y, and a or
            // b must be east of pt.x
        }

        double m = (aY - bY) / (aX - bX); // Rise over run
        double bee = (-aX) * m + aY; // y = mx + b
        double x = (pY - bee) / m; // algebra is neat!

        return x > pX;
    }

    @Override
    public void loadData(JSONObject jsonResponse) throws JSONException {
        System.out.println("tes"+jsonResponse.toString());
        jsonString = jsonResponse.getString("data");
        Log.d("data: ", jsonString);
        try {
            drawPolygon(jsonString);
        } catch (JSONException e) {
            System.err.println("Error = " + e.toString());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private String GenerateNum(){
        String text = "";
        String possible = "012345678";
        for (int i=0;i<1;i++){
            text += possible.charAt((int) Math.floor(Math.random()*possible.length()));
        }
        return text;
    }


}

kira kira apa yang menyebabkan force close dari code diatas, saya sudah coba perbaiki tapi masih belum dapat juga jalan keluarnya. Terima Kasih sebelumnya

Menampilkan 1-5 dari 5 hasil
handita okviyanto menjawab 4 tahun yang lalu :

coba dicek menggunakan logcat di devices.. kalau gak ada errornya susah mencarinya...

sambungkan devices ke komputer kemudian run di devices. Atau pake alogcat

kemudian perhatikan errornya di bagian yang mana


Yohanes Erwin menjawab 4 tahun yang lalu :

wah makasih, soalnya device trsebut gak kebaca di android studio saya, jadi gabisa liat logcatnya


Yohanes Erwin menjawab 4 tahun yang lalu :

udah saya coba gan, tapi log dr aplikasi saya gak kebaca di alogcat, gimana?


handita okviyanto menjawab 4 tahun yang lalu :

coba dicek ulang gan.. kalo gak pake fungsi bawaan dari log di android Log.i ataupun Log.e biar keluar lognya..


Muhammad Rifqi Priyo Susanto menjawab 3 tahun yang lalu :

Cek batasan minimal tingkat log, pastikan pilih DEBUG atau VERBOSE.

Log | Android Developers


Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 18
X 10

Bantuan

Help

Feedback

Help