Search code examples
javaandroidandroid-fragmentsandroid-viewpager

null object reference on set adapter


I know that there are a lot of question like this, but i can't find one that can help me. Please pay no attention at strings that are hard coded or other things like that, i will fix. I don't see anything wrong in adapter class or in the fragments. I use Disco class for fill a place list, the Place class contains all the methods that after i use in the adapter.Can anyone help me please? Thank you.

Class MainActivity

package com.techmind.tourguideapp;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

    FragmentManager manager = getSupportFragmentManager();
    ViewPager pager = findViewById(R.id.tourPager);
    pager.setAdapter(new FragmentAdapter(manager));

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(pager);
}
}

class Disco

package com.techmind.tourguideapp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nahuel on 31.05.2018.
 */

public class Disco {

public static ArrayList<Places> initDiscoList(ArrayList<Places> places) {
    places.add(new Places().
            setAddress("via a").
            setImage(1).
            setName("casino").
            setPhone("333").
            setPrice("20€").
            setHour("10:30"));

    return places;
}
}

Class DiscoFragment

package com.techmind.tourguideapp;

import android.support.v4.app.Fragment;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nahuel on 31.05.2018.
 */

public class DiscoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {

    ArrayList<Places> list = new ArrayList<>();
    list = Disco.initDiscoList(list);
    PlacesAdapter adapter = new PlacesAdapter(getActivity(), list);
    View view = inflater.inflate(R.layout.row_item, container, false);
    ListView listView = (ListView) view.findViewById(R.id.list_items);
    listView.setAdapter(adapter);

    return view;
}
}

Class FragmentAdapter

package com.techmind.tourguideapp;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by Nahuel on 31.05.2018.
 */

public class FragmentAdapter extends FragmentPagerAdapter {


public FragmentAdapter (FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return new DiscoFragment();
}

@Override
public int getCount() {
    return 3;
}

public CharSequence getPageTitle(int position) {
    return "Gradara";
}
}

Class Places

package com.techmind.tourguideapp;

/**
 * Created by Nahuel on 31.05.2018.
 */

public class Places {

private String name;
private String price;
private String phone;
private String Address;
private String hour;
private int image;
private final int NO_IMAGE = -1;

public Places () {
}

public String getHour() {
    return hour;
}

public String getName() {
    return name;
}

public String getPrice() {
    return price;
}

public String getPhone() {
    return phone;
}

public String getAddress() {
    return Address;
}

public int getImage() {
    return image;
}

public Places setName(String name) {
    this.name = name;
    return this;
}

public Places setHour(String hour) {
    this.hour = hour;
    return this;
}

public Places setPrice(String price) {
    this.price = price;
    return this;
}

public Places setPhone(String phone) {
    this.phone = phone;
    return this;
}

public Places setAddress(String address) {
    Address = address;
    return this;
}

public Places setImage(int image) {
    this.image = image;
    return this;
}
}

Class PlaceAdapter

package com.techmind.tourguideapp;

import android.content.Context;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class PlacesAdapter extends ArrayAdapter<Places> {

public PlacesAdapter(Context context,  List<Places> places) {
    super(context,-1, places);
}

public View getView (int position, ViewGroup parent, View convertView) {

    View listView = convertView;
    Places place = getItem(position);

    if(listView == null) {
        listView = LayoutInflater.from(getContext()).inflate(
                R.layout.row_item, parent, false);
    }


    TextView location = listView.findViewById(R.id.locationName);
    location.setText("casino");
    TextView hour = listView.findViewById(R.id.txthour);
    hour.setText("aaa");
    TextView address = listView.findViewById(R.id.address);
    address.setText("aaaaa");
    TextView price = listView.findViewById(R.id.txtmoney);
    price.setText("aaaaa");
    TextView phone = listView.findViewById(R.id.phone);
    phone.setText("aaaaa");
    ImageView img = listView.findViewById(R.id.imgRow);
    img.setImageResource(R.drawable.ic_launcher_background);

    return listView;
}
}

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.techmind.tourguideapp.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMaxWidth="0dp"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/tourPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

List_item.xml

</LinearLayout>

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

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

</ListView>

</LinearLayout>

Row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<ImageView
    android:id="@+id/imgRow"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/gradara" />

<TextView
    android:id="@+id/locationName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="24sp"
    android:textStyle="bold"
    android:text=""/>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0.5">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <ImageView
            android:id="@+id/positionIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/position"
            android:layout_alignParentStart="true"/>

        <TextView
            android:id="@+id/txtPosition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/positionIcon"
            android:layout_marginTop="5dp"
            android:text="" />

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/positionIcon"
            android:layout_toRightOf="@id/positionIcon"
            android:text="" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <ImageView
            android:id="@+id/moneyIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/money"
            android:layout_alignParentStart="true"/>

        <TextView
            android:id="@+id/txtmoney"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/moneyIcon"
            android:layout_marginTop="15dp"
            android:text="10€ - 20€" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <ImageView
            android:id="@+id/hourIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/hour"
            android:layout_alignParentStart="true"/>

        <TextView
            android:id="@+id/txthour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/hourIcon"
            android:layout_marginTop="15dp"
            android:text="09:30 - 23:00" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone:"
            android:layout_marginEnd="5dp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3334323560"
            />
    </LinearLayout>

</LinearLayout>



</LinearLayout>

Solution

  • i found the error in

    ArrayList<Places> list = new ArrayList<>();
    list = Disco.initDiscoList(list);
    PlacesAdapter adapter = new PlacesAdapter(getActivity(), list);
    View view = inflater.inflate(R.layout.row_item, container, false);
    ListView listView = (ListView) view.findViewById(R.id.list_items);
    listView.setAdapter(adapter);
    
    return view;
    }
    

    When i use the inflate method the first parameter was wrong, i used the layout of the row instead of the listView layout.

    Thank you for your answers, next times i will post logs and less useless code :)