0

Hey guys I tried do my source code work, but always I have this output:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Could help me?

 <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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Principal" >

<Button
    android:layout_width="73dp"
    android:layout_height="92dp"
    android:id="@+id/like"
    android:background="@drawable/like"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/rota" />

<Button
    android:layout_width="113dp"
    android:layout_height="82dp"
    android:text="Calcule sua rota"
    android:id="@+id/rota"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/like" />

<Button
    android:layout_width="76dp"
    android:layout_height="85dp"
    android:id="@+id/dislike"
    android:background="@drawable/dislike"
    android:layout_alignParentBottom="true"
    android:layout_toEndOf="@+id/rota"
    android:layout_alignTop="@+id/rota"
    android:layout_toRightOf="@+id/rota" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listLike" />

public void preecheListLike() {

    String[] motivoLike = new String[]{
            "Rampa de Acesso","Calçada sem Obstrução","Terreno Plano","Local com Acessibilidade","Ponto de Informações"
    };

    ArrayAdapter<String> adapterL = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, motivoLike);
    listaLike = (ListView) view.findViewById(R.id.listLike);
    listaLike.setAdapter(adapterL);

}

2 Answers2

0

Just call "findViewById(R.id.listLike)" without view. I think that should solve your issue.

faljbour
  • 1,367
  • 2
  • 8
  • 15
0
listaLike = (ListView) view.findViewById(R.id.listLike);

What is "view" here? This line, just find a view which has ID = listlike INSIDE the "view", if your "view" is not father view group of that ListView, your listview can not be found from this line of code. So, pls remove "view" and try.

And if you are using ListActivity, pls assign ListView ID = android.R.id.list (and @android:id/list in XML)

quocnhat7
  • 213
  • 1
  • 10