NAVIGATION DRAWER: - NullPointerException:尝试调用虚方法'java.lang.String android.content.Context.getPackageName()

时间:2016-03-22 19:27:16

标签: android

DRAWER IMAGE你好朋友我在我的项目中使用导航抽屉,但在尝试打开listview-listitem的活动时遇到上述错误(第二次)该应用程序在我们第一次运行时完美运行单击列表项,但如果我们返回并单击任何抽屉菜单项,之后如果我们尝试单击listitem,则会发生错误 LIST VIEW

i = 0
while ( i < 100){

datappointvalue = [ new date(archivetime[i]), archivevalue[i] ]  ;    
dataload.push(datapidvalue);
}

主要活动

FRAGMENT.java


package com.forever.technology.bscit;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.forever.technology.bscit.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class Sem1Fragment_list extends ListFragment {




    public Sem1Fragment_list() {
        // Required empty public constructor
    }
    ListView lv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sem1_list, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.sem1, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        switch (position)
        {
            case 0:
                Intent intent = new Intent(getContext(),Subject1Activity.class);
                startActivity(intent);
        }

    }
}

Subject1.activity

package com.forever.technology.bscit;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NavigationView navigationView = null;
    Toolbar toolbar = null;

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

        //Set the fragment initially
        HomeFragment fragment = new HomeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);

        //How to change elements in the header programatically

      /*  View headerView = navigationView.getHeaderView(0);
        TextView emailText = (TextView) headerView.findViewById(R.id.email);
        emailText.setText("newemail@email.com"); */

        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            //Set the fragment initially
            HomeFragment fragment = new HomeFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
            // Handle the camera action
        } else if (id == R.id.nav_sem1) {
            //Set the fragment initially
            Sem1Fragment fragment = new Sem1Fragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
            // Handle the camera action
        } else if (id == R.id.nav_sem2) {
            //Set the fragment initially
            Sem2Fragment fragment = new Sem2Fragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

        } else if (id == R.id.nav_sem3) {

        } else if (id == R.id.nav_sem4) {

        } else if (id == R.id.nav_sem5) {

        } else if (id == R.id.nav_sem6) {

        }
        else if (id == R.id.nav_blogs) {

        }
        else if (id == R.id.nav_site) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

logcat的

package com.forever.technology.bscit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Subject1Activity extends AppCompatActivity {

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

3 个答案:

答案 0 :(得分:3)

问题在于Sem1Fragment.java代码。 您正在使用膨胀视图的静态参考“rootView”。再次从NavigationDrawer中选择相同的片段,您再次实例化相同的片段,但由于您的膨胀视图引用是静态的,因此它不会被刷新并且将包含陈旧的上下文,因此getContext()为null 。 从而, 更改您的代码 -

static View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.fragment_sem1, container, false);
    }
    return rootView;
}

通过

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       return inflater.inflate(R.layout.fragment_sem1, container, false);
    }
  

还有一项改进

  • 您应该检查onNavigationItemSelected()您尝试实例化的片段是否已经可见,然后再不进行实例化。

答案 1 :(得分:1)

如果我的理解是正确的,您可以从Sem1Fragment开始使用不同的片段MainActivity。因此,具有列表Sem1Fragment_list的实际片段没有启动Subject1Activity所需的上下文。因此,只需将导航抽屉代码中的Sem1Fragment替换为Sem1Fragment_list,如下所示。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NavigationView navigationView = null;
    Toolbar toolbar = null;

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

        //Set the fragment initially
        HomeFragment fragment = new HomeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);

        //How to change elements in the header programatically

      /*  View headerView = navigationView.getHeaderView(0);
        TextView emailText = (TextView) headerView.findViewById(R.id.email);
        emailText.setText("newemail@email.com"); */

        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_about) {
            return true;
        }
        if (id == R.id.action_exit) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(false);
            builder.setMessage("Do you want to Exit?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //if user pressed "yes", then he is allowed to exit from application
                    finish();
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //if user select "No", just cancel this dialog and continue with app
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            //Set the fragment initially
            HomeFragment fragment = new HomeFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
            // Handle the camera action
        } else if (id == R.id.nav_sem1) {
            //Set the fragment initially
            Sem1Fragment_list fragment = new Sem1Fragment_list();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.addToBackStack(null); //add this to restore the fragment when back button is pressed
            fragmentTransaction.commit();
            // Handle the camera action
        } else if (id == R.id.nav_sem2) {
            //Set the fragment initially
            Sem2Fragment fragment = new Sem2Fragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        } else if (id == R.id.nav_sem3) {

        } else if (id == R.id.nav_sem4) {

        } else if (id == R.id.nav_sem5) {

        } else if (id == R.id.nav_sem6) {

        }
        else if (id == R.id.nav_blogs) {

            try {
                //If App installed
                Intent intent;
                intent = getPackageManager().getLaunchIntentForPackage("com.forevertech.app");
                startActivity(intent);
            } catch (Exception e) { //google play app is not installed
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.forevertech.app"));
                startActivity(intent);
            }
        }
        else if (id == R.id.nav_site) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://www.abaagekya.com/"));
            startActivity(intent);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

答案 2 :(得分:0)

请参阅网络上的任何导航抽屉示例。您必须在基本活动中实施Nav Drawer&amp;您的所有活动都应扩展BaseActivity。

这是我的工作代码:

<强> BaseActivity

package com.projects.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.ajay.projects.adapters.NavigationDrawerListAdapter;
import com.ajay.projects.models.Items;

import java.util.ArrayList;

/**
 * @author ajay.barve@gmail.com
 *         <p/>
 *         This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
 *         We are going to extend all our other activities from this BaseActivity so that every activity will have Navigation Drawer in it.
 *         This activity layout contain one frame layout in which we will add our child activity layout.
 */
public class BaseActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    /**
     * Static variable for selected item position. Which can be used in child activity to know which item is selected from the list.
     */
    protected static int position;
    /**
     * This flag is used just to check that launcher activity is called first time so that we can open appropriate Activity on launch and make list item position selected accordingly.
     */
    private static boolean isLaunch = true;
    /**
     * Frame layout: Which is going to be used as parent layout for child activity layout.
     * This layout is protected so that child activity can access this
     */
    protected FrameLayout frameLayout;
    /**
     * ListView to add navigation drawer item in it.
     * We have made it protected to access it in child class. We will just use it in child class to make item selected according to activity opened.
     */

    protected ListView mDrawerList;
    /**
     * List item array for navigation drawer items.
     */
    protected String[] listArray = {"About CML Dashboard", "About Jaslok Onchology Department", "View Report", "Add Record", "CBC Trend", "Tabular Report"};
    protected ArrayList<Items> _items;
    boolean dataExists = false;
    /**
     * Base layout node of this Activity
     */
    private DrawerLayout mDrawerLayout;
    /**
     * Drawer listner class for drawer open, close etc.
     */
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

        frameLayout = (FrameLayout) findViewById(R.id.content_frame);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _items = new ArrayList<Items>();
        _items.add(new Items("About Jaslok Hospital", "Lean about Jaslok Hospital & Jaslok onchology department", R.drawable.item_1));
        _items.add(new Items("View Report", "View CBC Reports stored int he device", R.drawable.item_2));
        _items.add(new Items("Add Record", "Add a new CBC Report", R.drawable.item_3));
        _items.add(new Items("CBC Trend", "View trends of each parameter in CBC Report", R.drawable.item_4));
        _items.add(new Items("Tabular Report", "View tabular   CBC Report", R.drawable.item_5));
        //Adding header on list view
        View header = getLayoutInflater().inflate(R.layout.list_view_header_layout, null);
        mDrawerList.addHeaderView(header);

        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new NavigationDrawerListAdapter(this, _items));
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                openActivity(position);
                Log.e("Logger:BaseActivity", "onItemClick-Enter");
            }
        });

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);


        // ActionBarDrawerToggle ties together the the proper interactions between the sliding drawer and the action bar app icon
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                       /* host Activity */
                mDrawerLayout,              /* DrawerLayout object */
                R.drawable.aj_launcher,     /* nav drawer image to replace 'Up' caret */
                R.string.open_drawer,       /* "open drawer" description for accessibility */
                R.string.close_drawer)      /* "close drawer" description for accessibility */ {
            @Override
            public void onDrawerClosed(View drawerView) {
                getActionBar().setTitle(listArray[position]);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(getString(R.string.app_name));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                super.onDrawerStateChanged(newState);
            }
        };
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

    }

    /*End of OnCreate*/

    /*Start OnClick*/
    /* Comment : Have to implement with the OnClickListner onClick is called when a view has been clicked.*/
    public void onClick(View v) { // Parameter v stands for the view that was clicked.

        Log.e("Logger:BaseActivity", "onClick:Enter");
        final Context context = this;
    }
    /*End of onClick*/

    /**
     * @param position Launching activity when any list item is clicked.
     */
    protected void openActivity(int position) {
        Log.e("Logger:BaseActivity", "openActivity-Enter");
        /**
         * We can set title & itemChecked here but as this BaseActivity is parent for other activity,
         * So whenever any activity is going to launch this BaseActivity is also going to be called and
         * it will reset this value because of initialization in onCreate method.
         * So that we are setting this in child activity.
         */
//      mDrawerList.setItemChecked(position, true);
//      setTitle(listArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.

        switch (position) {
            case 0:
                startActivity(new Intent(this, HomeActivity.class));
                break;
            case 1:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case 3:
                startActivity(new Intent(this, AddActivity.class));
                break;
            case 4:
                startActivity(new Intent(this, TrendActivity.class));
                break;
            case 5:
                startActivity(new Intent(this, TabularActivity.class));
                break;
            default:
                break;
        }

//      Toast.makeText(this, "Selected Item Position::"+position, Toast.LENGTH_LONG).show();
    }

    //@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.e("Logger:BaseActivity", "onCreateOptionsMenu-Enter");
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Logger:BaseActivity", "onOptionsItemSelected-Enter");
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            Log.e("Logger:BaseActivity", "onOptionsItemSelected-Enter"+item);
            return true;
        }

        switch (item.getItemId()) {
            case R.id.action_settings:
                Log.e("Logger:BaseActivity", "onOptionsItemSelected-getItemId"+item.getItemId());

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /* Called whenever we call invalidateOptionsMenu() */
    //@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.e("Logger:BaseActivity", "onPrepareOptionsMenu-Enter");
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /* We can override onBackPressed method to toggle navigation drawer*/
    @Override
    public void onBackPressed() {
        Log.e("Logger:BaseActivity", "onBackPressed-Enter");
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Log.e("Logger:BaseActivity", "onItemSelected-Enter");

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

}

<强> HomeActivity

package com.projects.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.ajay.projects.adapters.HomeScreenGridViewAdapter;

/**
 * @author ajay.barve@gmail.com
 */
public class HomeActivity extends BaseActivity {


    //private GridView gridview;
    private ListView gridview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * We will not use setContentView in this activty Rather than we will
         * use layout inflater to add view in FrameLayout of our base activity
         * layout
         */

        /**
         * Adding our layout to parent class frame layout.
         */
        getLayoutInflater().inflate(R.layout.home_layout, frameLayout);

        /**
         * Setting title and itemChecked
         */
        mDrawerList.setItemChecked(position, true);
        setTitle(listArray[position]);

        initializeActivity();
    }

    private void initializeActivity() {

        gridview = (ListView) findViewById(R.id.gridview);
        gridview.setAdapter(new HomeScreenGridViewAdapter(this, _items));
    }
}

代码是不言自明的。希望这个例子有所帮助。