调用gridview.setAdapter()时java.lang.NullPointerException

时间:2014-01-30 10:28:20

标签: android

this.gridQuickKeys.setAdapter(localQuickKeysAdapter)中调用MainActivity方法时,应用程序返回java.lang.nullpointerexception。我该如何解决?

这是MainBaseActivity

public class MainBaseActivity extends Activity {
public Context context;
public LinearLayout mainContainer;
public RelativeLayout dataContainer;
protected LinearLayout titleContainer;

@Override
public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.baseactivity);
    this.context = this;
    getAllViews();
}

private void getAllViews() {
    mainContainer = (LinearLayout) findViewById(R.id.linear_layout);
    dataContainer = (RelativeLayout) findViewById(R.id.dataContainer);
    titleContainer = (LinearLayout) findViewById(R.id.titleContainer);

    LinearLayout.LayoutParams localLayoutParams = new   LinearLayout.LayoutParams(
            -1, -1);
    this.dataContainer.setLayoutParams(localLayoutParams);      
}
}

这是MainActivity

public class MainActivity extends MainBaseActivity {

private LinearLayout sellCurrentMain;
public static Context context;

private GridView gridQuickKeys;
public static ListView lvprdcurrentsell;

private QuickKeysAdapter localQuickKeysAdapter; 

private sellProductLogic _sellProductLogic;
private List<sellProductInfo> _sellProductInfo;

List<String> quickimages = new ArrayList();
List<String> quicknames = new ArrayList();

private LayoutInflater layoutInflater;

public static List<sellOrderDetailInfo> _listsellOrderDetailInfo = new ArrayList();
public static sellOrderDetailInfo _sellOrderDetailInfo = new sellOrderDetailInfo();
public static PrdSellListAdapter _PrdSellListAdapter;; // for sell listview

public static List<stockbeansale> listsby = new ArrayList();
public static stockbeansale _stockStockbeansale = new stockbeansale();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    context = this;
    this.layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    this.sellCurrentMain = ((LinearLayout) this.layoutInflater.inflate(
            R.layout.sellcurrentsell, null));
    LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
            -1, -1);
    this.sellCurrentMain.setLayoutParams(localLayoutParams);

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
        moveTaskToBack(true);
    }
    _sellProductLogic = new sellProductLogic(this);     

    _sellProductInfo = _sellProductLogic.getAllsellProductDataLogic();              

    this.gridQuickKeys = (GridView) this
            .findViewById(R.id.gridViewQuickKeys);
    MainActivity.lvprdcurrentsell = (ListView) this
            .findViewById(R.id.lvprdcurrentsell);

    refreshGrid();this.dataContainer.addView(this.sellCurrentMain);

}

protected void refreshGrid() {
    while (true) {
        try {
            localQuickKeysAdapter = new QuickKeysAdapter(context,
                    _sellProductInfo, this.quickimages);
            this.gridQuickKeys.setAdapter(localQuickKeysAdapter);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

您需要先致电setContentView(R.layout.your_layout)

this.gridQuickKeys = (GridView) this.findViewById(R.id.gridViewQuickKeys);

如果gridViewQuickKeys位于R.layout.sellcurrentsell,则需要致电

this.gridQuickKeys = (GridView) sellCurrentMain.findViewById(R.id.gridViewQuickKeys);

而不是

this.gridQuickKeys = (GridView) sellCurrentMain.findViewById(R.id.gridViewQuickKeys);