【问题标题】:Passing ArrayList<Double> to another class将 ArrayList<Double> 传递给另一个类
【发布时间】:2018-08-28 12:09:28
【问题描述】:

我可以使用我的代码读取文本文件并将其保存到Arraylist。现在我想在其他活动中使用这个Arraylist,但是我在将Arraylist 传递给另一个类时遇到了问题。我故意尝试过,但没有奏效。我将在下面添加我的程序的代码。

Intent intent=new Intent(this, ergebnisse.class);
intent.putExtra("NEFZ", Nlist);
startActivity(intent);

我希望你能帮助我。

我的第一个活动:

public class NEFZ2array extends AppCompatActivity implements Serializable {

public static void main(String[] args) {
    FileReader file = null;
    try {
        file = new FileReader("NEFZ.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ArrayList<Double> Nlist = new ArrayList<Double>();
    int i=0;
    Double d= null;
    try {
        BufferedReader input = new BufferedReader(file);
        String s=null;
        while((s=input.readLine())!=null) {
            StringTokenizer st = new StringTokenizer(s,",");
            while(st.hasMoreTokens()) {
                try {
                    d = Double.parseDouble(st.nextToken());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Nlist.add(i, d);
            }
        }
        input.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
    for(double j:Nlist) {
        System.out.println(j);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nefz2array);

}
}

我的接收者活动:

public class ergebnisse extends AppCompatActivity implements Serializable {

public Button button_ausfuerlicher;
public Button button_home;

public void init(){
    button_ausfuerlicher = (Button) findViewById(R.id.button_ausfuerlicher);
    button_ausfuerlicher.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(ergebnisse.this, ausfuehrlicher.class);

            startActivity(intent);
        }
    });
}

public void init2(){
    button_home = (Button) findViewById(R.id.button_home);
    button_home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(ergebnisse.this, MainActivity.class);

            startActivity(intent);
        }
    });
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ergebnisse);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    init();
    init2();
}
}

【问题讨论】:

  • Android 应用程序的入口点通常是 onCreate 方法。我怀疑您的主要方法(传统的 java 入口点)没有运行。您可以尝试从 onCreate 调用它,但实际上应该重命名它。要接收 Intent(只要您调用 main 并添加它就应该发送)确保接收者覆盖 onReceive
  • “没有工作”是什么意思
  • psvm() 不是 android 活动生命周期的一部分,因此永远不会被调用。
  • 您好,Ugur,您的代码不完整,因为您放置/发送了额外的,但您在第二个活动中没有获取/接收它。检查这个:stackoverflow.com/a/5265952/10005752,祝你好运!
  • public static void main(String[] args) { 这不是 Android 的工作方式。在继续之前,您似乎需要一些基本的 Android 教程

标签: java android android-intent arraylist


【解决方案1】:

您需要在接收器活动中调用 getIntent()。

Intent intent = getIntent();

然后你就可以打电话了

intent.getExtras()

【讨论】:

    【解决方案2】:

    Android 应用程序的入口点通常是 onCreate 方法。

    我怀疑您的主要方法(传统的 java 入口点)没有运行。您可以尝试从 onCreate 中调用它,但实际上应该重命名它。

    要接收应发送的 Intent,只要您调用 main 并添加 Intent 代码,请确保接收者覆盖 onReceive。

    @Override
    public void onReceive(Context context, Intent intent) {
        ArrayList<Double> NEFZ = intent.getExtras().getString("NEFZ");
    }
    

    【讨论】:

    • 奇怪的是 main 方法正在运行并且输出是正确的。如果我将代码更改为 onCreate 则会出现错误。运行“NEFZ2array”时出错:必须导出活动或包含意图过滤器。所以我看不到代码是否正确。
    • 它是在您的 IDE、模拟器还是设备上运行?
    • 用于测试该类是否在 IDE 上运行。但是该应用程序在真实设备上运行没有任何问题。
    • 我建议在你的 main 方法中添加一个 toast 或类似的方法,以检查它在运行你的应用程序时是否实际执行。
    猜你喜欢
    • 2013-12-10
    • 2021-05-06
    • 2018-06-12
    • 2019-05-27
    • 2016-01-14
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多