-4

I am creating simple listview from string array. i watched many tutorials all are saying the same just 3 lines enough to make it work.I am doing the same but still getting NPE , almost frustrated with this issue. Could not figure out whats went wrong please help me.

My code is here:

Mainfiest:

   <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />

MainActivity.java

public class MainActivity extends Activity {
    ListView lv;
    String[] days = new String[] { "Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, days);
        lv.setAdapter(adapter);
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Ashok
  • 3
  • 2

4 Answers4

1

Hey are you wrong configure code. here you are put listview in fragment xml and write your code in mainactivity class so you have to either remove your fragment xml and Replace your fragment xml code to mainactivity xml and Remove below code

if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

from onCreate()

OR

Just Initialize your Listview in OnCreateView of Fragment Class

public static class PlaceholderFragment extends Fragment {
    ListView lv;
String[] days = new String[] { "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday"};

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
            lv = (ListView) view.findViewById(R.id.listView1);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(GetActivity(),
            android.R.layout.simple_list_item_1, days);

            lv.setAdapter(adapter);
        return rootView;
    }
}

Thats it...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

change

setContentView(R.layout.activity_main);

to

setContentView(R.layout.fragment_main);
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

Your showing new fragment in your activity.

if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
}

But listview is declared in main activity layout. So to view your list view just comment above code and see.

Kadari
  • 311
  • 1
  • 3
  • 5
0

You need to add items in your "String[] days" to your "adapter" you can use the methods adapter.add or adapter.addAll for adding items to your ArrayAdapter.for more details here is an example of simple listView

http://windrealm.org/tutorials/android/android-listview.php

nvinayshetty
  • 3,187
  • 1
  • 21
  • 32