0

I want to show a list view that contains images,text,numbers in a fragment in a

tab. The name of the fragment is "home1.java" .

I have a method displaydatabaseinfo() in mainactivity.java which serves the purpose.

There is also a class named "TabFragment.java" which controls the flow.

The line " ListView lvlogItems = (ListView) findViewById(R.id.listView);" is returning a null and hence the data is not being shown.

This is the following error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chven.swipeviewtest/com.example.chven.swipeviewtest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Below is the code for method mainactivity.java

public class MainActivity extends AppCompatActivity {

LogReader LogReader1 = new LogReader(this);
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTrasaction;

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


    try {
        get();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }



    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
    mFragmentManager = getSupportFragmentManager();
    mFragmentTrasaction = mFragmentManager.beginTransaction();
    mFragmentTrasaction.replace(R.id.containerView, new TabFragment()).commit();
   displayDatabaseInfo();
}

public void displayDatabaseInfo() {
    // To access our database, we instantiate our subclass of SQLiteOpenHelper
    // and pass the context, which is the current activity.
    SQLiteDatabase db = LogReader1.getReadableDatabase();
    Cursor logCursor = db.rawQuery("SELECT rowid _id,name,Time_Used,app_Image FROM logs ORDER BY Time_Used DESC", null);
    LogCursorAdapter logAdapter = new LogCursorAdapter(this, logCursor);
    ListView lvlogItems = (ListView)  findViewById(R.id.listView);
    lvlogItems.setAdapter((logAdapter));
}

This is the code for the fragment "home1.java"

public class home1 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;



public home1() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment home1.
 */
// TODO: Rename and change types and number of parameters
public static home1 newInstance(String param1, String param2) {
    home1 fragment = new home1();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    return inflater.inflate(R.layout.fragment_home1, container, false);

}

}

This is the code for "TabFragment.java"

public class TabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 1;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View x = inflater.inflate(R.layout.tab_layout, null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);

    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;
}
class MyAdapter extends FragmentPagerAdapter{

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

    @Override
    public Fragment getItem(int position)
    {
        switch (position) {
            case 0 : return new home1();

        }
        return null;
    }

    @Override
    public int getCount() {

        return int_items;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "Home";


        }
        return null;
    }
}

}

NIT543HIN
  • 9
  • 3
  • 1
    Please post a [MCVE] – c0der Apr 02 '17 at 03:49
  • It looks you have ListView defined in your fragment layout i.e. R.layout.fragment_home1 but you are trying to initialize/populated directly from the Mainactivity... you need to initialize and set the adapter inside the Fragment i.e home1.java – MSC Apr 02 '17 at 03:51

1 Answers1

0

I'd suggest moving your call to displayDatabaseInfo() into the onResume() of the Activity: you're probably getting a null pointer because onCreateView in the fragment hasn't yet been called.

Femi
  • 64,273
  • 8
  • 118
  • 148