2014-01-30 35 views
2

當在MainActivity中調用this.gridQuickKeys.setAdapter(localQuickKeysAdapter)方法時,應用程序返回java.lang.nullpointerexception。我該如何解決它?java.lang.NullPointerException當調用gridview.setAdapter()

這是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; 
     } 
    } 
} 
} 
+0

哪裏是你的活動的setContentView? – Raghunandan

+0

確定帶有'R.id.gridViewQuickKeys'的GridView是'R.layout.baseactivity'的一部分嗎? – WarrenFaith

回答

1

你需要調用setContentView(R.layout.your_layout) beforey你可以,如果你的gridViewQuickKeysR.layout.sellcurrentsell需要調用做

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

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

代替

this.gridQuickKeys = (GridView) sellCurrentMain.findViewById(R.id.gridViewQuickKeys); 
相關問題