2017-04-26 14 views
-1

我有一個場景,我創建了一個庫項目,在其中加載佈局以及定義我想從應用程序項目中調用的方法。活動方法在android中從其他類中創建之前獲取調用

public class LibraryActivity { 


public LibraryActivity() { 

} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.barchart_layout); 
    bindViews(); 

} 


    public void bindViews() { 

     bChart = (BarChart)findViewById(R.id.barchart); 
} 

    public void setData(int count, float range) { 

    //definition of the method 
    // I have to user bChart view here 
    System.out.println("part 1 "+bChart); <---- this is null 
} 

現在,我已經成功地創建了AAR的項目,我想利用這個使用setData從其他項目。 所以,當我跑這從另一個項目通過使用下面的代碼

Intent in = new Intent(MainActivity.this,LibraryActivity.class); 
      startActivity(in); 

      LibraryActivity barChartCallBack = new LibraryActivity(); 
      LibraryActivity.setData(15,25); 

我上了bChart空指針,因爲我的方法使用setData有先打電話之前的OnCreate所以findviewbyid給我bChart

+0

通過'Bundle'傳遞數據。 – azizbekian

+0

SetData方法有該視圖必須加載此方法獲取調用之前我能夠傳遞數據,但因爲視圖不加載和方法獲取調用優先,所以我得到空指針 –

回答

3

您正在將數據設置爲完全不同的LibraryActivity對象。首先告訴系統意圖啓動一個,然後啓動另一個不顯示的系統,但是您使用它來調用setData。

在Android開發者網站上閱讀起來:

https://developer.android.com/training/basics/firstapp/starting-activity.html

基本上你需要通過與意圖通過額外的數據。

+0

我知道,但我這裏的問題是方法我在庫類中獲取調用優先[因爲我從另一個項目調用此方法]和oncreate後加載,因爲我無法得到我必須在setData方法中使用的視圖。 –

+0

不要從外部調用setData,請改用in.putExtra(...) –

相關問題