繁体   English   中英

Activity方法在android中的oncreate之前从其他类获取调用

[英]Activity method gets call before oncreate in android from the other class

我有一个方案,其中我创建了库项目,在其中加载布局以及定义要从Application项目中调用的方法。

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。

您正在完全不同的LibraryActivity对象上设置数据。 首先,您告诉系统启动一个系统的意图,然后启动另一个未显示的系统,但是使用它来调用setData。

在android开发人员网站上阅读:

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

基本上,您需要根据意图通过Extras传递数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM