Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8840983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:35:54+00:00 2026-06-14T10:35:54+00:00

When I insert a listview in a fragment in my application, it doesn’t show

  • 0

When I insert a listview in a fragment in my application, it doesn’t show up after I populate it with items. In fact, the application crashes due to a NullPointerException. Can anybody help me? Here is the detail activity from which I show the fragments.

package com.example.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

/**
 * An activity representing a single Course detail screen. This activity is only
 * used on handset devices. On tablet-size devices, item details are presented
 * side-by-side with a list of items in a {@link CourseListActivity}.
 * <p>
 * This activity is mostly just a 'shell' activity containing nothing more than
 * a {@link CourseDetailFragment}.
 */
public class CourseDetailActivity extends SherlockFragmentActivity {

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

    // Show the Up button in the action bar.
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // initiating both tabs and set text to it.
    ActionBar.Tab assignTab = actionBar.newTab().setText("Assignments");
    ActionBar.Tab schedTab = actionBar.newTab().setText("Schedule");
    ActionBar.Tab contactTab = actionBar.newTab().setText("Contact");

    // Create three fragments to display content
    Fragment assignFragment = new Assignments();
    Fragment schedFragment = new Schedule();
    Fragment contactFragment = new Contact();

    assignTab.setTabListener(new MyTabsListener(assignFragment));
    schedTab.setTabListener(new MyTabsListener(schedFragment));
    contactTab.setTabListener(new MyTabsListener(contactFragment));

    actionBar.addTab(assignTab);
    actionBar.addTab(schedTab);
    actionBar.addTab(contactTab);

    ListView listView = (ListView) findViewById(R.id.assignlist);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };

    // First paramenter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpTo(this, new Intent(this,
                CourseListActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;
    public Fragment fragment2;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.main_across, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}
}

The fragment that I am currently trying to get working is called the Assignments fragment. As you can see in the CourseDetailActvity, I populate smaple items in the listview to see if it the listview shows up. The fragment gets inflated properly, but when I try to add items to the listview, the application crashes!

Here is the logcat.

11-17 11:54:28.037: E/AndroidRuntime(282): FATAL EXCEPTION: main
11-17 11:54:28.037: E/AndroidRuntime(282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sample/com.example.sample.CourseDetailActivity}: java.lang.NullPointerException
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.os.Looper.loop(Looper.java:123)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-17 11:54:28.037: E/AndroidRuntime(282):  at java.lang.reflect.Method.invokeNative(Native Method)
11-17 11:54:28.037: E/AndroidRuntime(282):  at java.lang.reflect.Method.invoke(Method.java:521)
11-17 11:54:28.037: E/AndroidRuntime(282):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-17 11:54:28.037: E/AndroidRuntime(282):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-17 11:54:28.037: E/AndroidRuntime(282):  at dalvik.system.NativeStart.main(Native Method)
11-17 11:54:28.037: E/AndroidRuntime(282): Caused by: java.lang.NullPointerException
11-17 11:54:28.037: E/AndroidRuntime(282):  at com.example.sample.CourseDetailActivity.onCreate(CourseDetailActivity.java:66)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 11:54:28.037: E/AndroidRuntime(282):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-17 11:54:28.037: E/AndroidRuntime(282):  ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:35:55+00:00Added an answer on June 14, 2026 at 10:35 am

    I believe your crash is on this line:

    listView.setAdapter(adapter);
    

    adapter obviously is not null (the crash is not within setAdapter() anyway), so listView must be null.

    ListView listView = (ListView) findViewById(R.id.assignlist);
    

    Well, we can see it declared there. However, findViewById() can return null if the object is not found. There appears to be no object (and in particular, no ListView) with ID assignlist within the layout you are using (activity_course_detail.xml).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a Listview EDIT/Insert template where I can use a checkbox
I've never used the ListView control before and am trying to programmatically insert items
I was trying to insert items in a listview in a database. If I
I have a listview that can contain up to roughly 2,000 listview items I
I want to refresh the listView after an insert or delete in the database..
I want to call a listview adapter from another activies after insert record to
How can I insert an OnClickListener for list view? I want to show an
I am trying to insert an image in ListView and when I click on
I'm trying to insert a ListView as one of the tabs in a tabhost.
When I click a button, I am trying to insert a new listview item.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.