Sagar
Sagar

Reputation: 60

custom listview is not working with new update of ADT

I'm just beginning Android development, and I'm working to get a Custom listview. according to this tutorial code is fine and working on Activity Class but if i am working by extending ActionBarActivity, this is not working and

MainActivity class code is :

public class MainActivity extends ActionBarActivity implements OnItemClickListener {
ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        MyAdapter adapter = new MyAdapter(this, generateData());
        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    }

    private ArrayList<Item> generateData() {
        ArrayList<Item> items = new ArrayList<Item>();
        items.add(new Item("Item 1", "First Item on the list", "100%"));
        items.add(new Item("Item 2", "Second Item on the list", "99%"));
        // items.add(new Item("Item 3","Third Item on the list"));

        return items;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    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;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "You Clicked at " + position , Toast.LENGTH_SHORT).show();
    }

}

log cat

06-09 16:36:40.076: E/AndroidRuntime(23808): FATAL EXCEPTION: main
06-09 16:36:40.076: E/AndroidRuntime(23808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example/com.example.example.MainActivity}: java.lang.NullPointerException
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.os.Looper.loop(Looper.java:137)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread.main(ActivityThread.java:4517)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at java.lang.reflect.Method.invoke(Method.java:511)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at dalvik.system.NativeStart.main(Native Method)
06-09 16:36:40.076: E/AndroidRuntime(23808): Caused by: java.lang.NullPointerException
06-09 16:36:40.076: E/AndroidRuntime(23808):    at com.example.example.MainActivity.onStart(MainActivity.java:90)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.Activity.performStart(Activity.java:4528)
06-09 16:36:40.076: E/AndroidRuntime(23808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1941)
06-09 16:36:40.076: E/AndroidRuntime(23808):    ... 11 more

My full code is here

Upvotes: 0

Views: 101

Answers (2)

Yash Sampat
Yash Sampat

Reputation: 30611

Your onCreate() method should look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

}

And add the following method to your MainActivity:

@Override
protected void onStart (){

    super.onStart();
    MyAdapter adapter = new MyAdapter(this, generateData());
    list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);

}

Upvotes: 1

deep
deep

Reputation: 169

use this code for updating list view

     list.setAdapter(null);

// new updated adpter
       MyAdapter adapter = new MyAdapter(this, generateData());
        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);

Upvotes: 0

Related Questions