10
Мар
2017

java.lang.RuntimeException: Unable to start activity ComponentInfo

Использую API при помощи Retrofit в Android приложении. Судя по ошибке не может определить где activity main. Помогите найти ошибку, а то в упор не вижу.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    List<PostModel> posts;

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

        posts = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.posts_recycle_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        PostsAdapter adapter = new PostsAdapter(posts);
        recyclerView.setAdapter(adapter);

        try {
            Response response = App.getApi().getData("bash", 180).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        App.getApi().getData("bash", 180).enqueue(new Callback<List<PostModel>>() {
            @Override
            public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) {
                posts.addAll(response.body());
                recyclerView.getAdapter().notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<List<PostModel>> call, Throwable t) {
                Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

activity_maim.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gercevoa.myapplication.MainActivity">>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/posts_recycle_view"
        android:layout_alignParentStart="true" />
</RelativeLayout>

post_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/postitem_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Очень интересный пост с баша, который никто никогда не видел, так как его не существует"
        android:textColor="?android:attr/textColorPrimary"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/postitem_site"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bash.im"
        android:layout_below="@+id/postitem_post"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:gravity="end"
        android:textAlignment="textEnd" />
</RelativeLayout>

И собственно ошибка

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gercevoa.myapplication/com.example.gercevoa.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call 

com.example.gercevoa.myapplication.UmoriliApi.getData(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.example.gercevoa.myapplication.UmoriliApi.getData(java.lang.String, int)' on a null object reference
at com.example.gercevoa.myapplication.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Источник: https://ru.stackoverflow.com/questions/637590/java-lang-runtimeexception-unable-to-start-activity-componentinfo

Share

Тебе может это понравится...