Soru & Cevap

"E/AndroidRuntime: FATAL EXCEPTION: main Process:" Hatası

29.06.2018 - 09:03

Kodlarım bunlar;

PersonsAdapter.java


package com.androidevreni.jsontutorial.adapters;

package com.androidevreni.jsontutorial.adapters;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidevreni.jsontutorial.R;
import com.androidevreni.jsontutorial.models.Person;

import java.util.List;


/**
 * Created by mustafademir on 19.06.2016.
 */
public class PersonsAdapter extends RecyclerView.Adapter<PersonsAdapter.MyViewHolder> {

    private List<Person> personsList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, city, country;
        public RelativeLayout renk;

        public MyViewHolder(View view) {
            super(view);
          //  name = (TextView) view.findViewById(R.id.atur);
            city = (TextView) view.findViewById(R.id.atur);
            country = (TextView) view.findViewById(R.id.icerik);
            renk = (RelativeLayout) view.findViewById(R.id.list_item_id);
        }
    }


    public PersonsAdapter(List<Person> personsList) {
        this.personsList = personsList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Person person = personsList.get(position);
        //holder.name.setText(person.name);
        holder.city.setText(person.city);
        holder.country.setText(person.country);
        holder.renk.setBackgroundColor(Color.parseColor(person.renk));
    }

    @Override
    public int getItemCount() {
        return personsList.size();
    }

}

Person.java
 

package com.androidevreni.jsontutorial.models;

import com.google.gson.annotations.SerializedName;

/**
 * Created by mustafademir on 17.06.2016.
 */
public class Person {

    @SerializedName("id")
    public String name;
    @SerializedName("atur")
    public String city;
    @SerializedName("icerik")
    public String country;
    @SerializedName("renk")
    public String renk;

    public Person(String name, String city, String country, String renk) {
        //this.name       = name;
        this.city       = city;
        this.country    = country;
        this.renk       = renk;
    }

}

MyApplication.java

package com.androidevreni.jsontutorial;

import android.app.Application;

import timber.log.Timber;

/**
 * Created by mustafademir on 13.06.2016.
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // TIMBER SETUP
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }

    }
}

MainApplication.java

package com.androidevreni.jsontutorial;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.androidevreni.jsontutorial.adapters.PersonsAdapter;
import com.androidevreni.jsontutorial.models.Person;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

public class MainActivity extends AppCompatActivity {

    private String JSON_URL = "http://10.0.2.2/database1/api/stock_service.php";
    private OkHttpClient client = new OkHttpClient();
    private List<Person> personList = new ArrayList<>();
    private RecyclerView recyclerView;
    private PersonsAdapter personsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Timber.i("onCreate ..");

        // JSON DATAYI İNDİR
        new DownloadJSON().execute();

        // RECYCLERVIEW
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        personsAdapter = new PersonsAdapter(personList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(personsAdapter);

    }

    /**
     * OkHttp İLE ADRESTEKİ VERİYİ İNDİREN METODUMUZ
     *
     * @param url
     * @return
     * @throws IOException
     */
    private String run(String url) throws IOException {
        Timber.i("run ..");
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    /**
     * JSON DATAYI ARKAPLANDA İNDİRMEMİZİ SAĞLAYACAK DownloadJSON ASYCTASK SINIFIMIZ
     */
    private class DownloadJSON extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            Timber.i("onPreExecute ..");

        }

        @Override
        protected String doInBackground(String... params) {
            Timber.i("doInBackground ..");

            // İNDİRME İŞİNİ BURADA YAPACAĞIZ
            String jsonData = null;
            try {
                jsonData = run(JSON_URL);
                Timber.i("JSON VERİMİZ : > " + jsonData);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // JSON VERİYİ NULL DEĞİLSE JAVA NESNELERİNE DÖNÜŞTÜR
            if (null != jsonData) {
                Timber.i("jsonData null değil ..");
                // VERİYİ GSON İLE NESNELERE DÖNÜŞTÜR
                Gson gson = new Gson();
                List<Person> persons = new ArrayList<Person>();
                persons = Arrays.asList(gson.fromJson(jsonData, Person[].class));

                // LİSTEYİ KONTROL AMAÇLI LOGCAT E YAZDIR & LİSTEYE AİT DİZİYE ELEMANLARI EKLE
                for (int i = 0; i < persons.size(); i++) {
                    Timber.i("name #" + i + " : > " + persons.get(i).name);
                    Timber.i("city #" + i + " : > " + persons.get(i).city);
                    Timber.i("country #" + i + " : > " + persons.get(i).country);
                    Timber.i("renk #" + i + " : > " + persons.get(i).renk);
                    personList.add(persons.get(i));
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Timber.i("onPostExecute ..");
            personsAdapter.notifyDataSetChanged();
        }
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/list_item_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F5A384"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp">

    <!-- ANA TÜR -->
    <TextView
        android:id="@+id/atur"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textAlignment="center"
        android:text="Ana Tür"
        android:textColor="#FFFFFF"
        android:textSize="20dp"
        android:textStyle="bold" />

    <!-- RESİM -->
    <LinearLayout
        android:id="@+id/resimBolum"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/atur"
        android:gravity="center">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/resim1" />

    </LinearLayout>

    <!-- İÇERİK -->
    <TextView
        android:id="@+id/icerik"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/resimBolum"
        android:text="ülke"
        android:textAlignment="center"
        android:textColor="#FFFFFF"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.androidevreni.jsontutorial.MainActivity">

        <!-- LİSTEMİZ -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" />

</RelativeLayout>

Aldığım hata bu;

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.androidevreni.jsontutorial, PID: 8245
    java.lang.NullPointerException: Attempt to invoke virtual method 'char java.lang.String.charAt(int)' on a null object reference
        at android.graphics.Color.parseColor(Color.java:1384)
        at com.androidevreni.jsontutorial.adapters.PersonsAdapter.onBindViewHolder(PersonsAdapter.java:55)
        at com.androidevreni.jsontutorial.adapters.PersonsAdapter.onBindViewHolder(PersonsAdapter.java:20)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
        at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1180)
        at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1031)
        at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1529)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2486)
        at android.view.View.dispatchTouchEvent(View.java:11776)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2962)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2643)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
        at android.view.View.dispatchPointerEvent(View.java:12015)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
06-29 17:56:38.437 8245-8245/com.androidevreni.jsontutorial E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:177)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6735)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6787)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:652)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

5 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-259959-1585770739.jpg
ertan.kocakoc
01.12.2019 - 03:02

Merhaba, http://stackoverflow.com/questions/10694310/fatal-exception-main-error-android-development inceleyebilirsiniz.