我有一個場景,我創建了一個庫項目,在其中加載佈局以及定義我想從應用程序項目中調用的方法。活動方法在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。
通過'Bundle'傳遞數據。 – azizbekian
SetData方法有該視圖必須加載此方法獲取調用之前我能夠傳遞數據,但因爲視圖不加載和方法獲取調用優先,所以我得到空指針 –