1

I can read and save a textfile to an Arraylist with my code. Now I want to use this Arraylist in other activities but I have problems with passing the Arraylist to another class. I tried it with intent but it didnt work. I will add the codes of my program below.

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

I hope you can help me.

My first activity:

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);

}
}

My Receiver Activity:

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();
}
}
ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    The entry point to an android application is typically the onCreate method. I suspect that your main method (traditional java entry point) is not being run. You could try calling it from the onCreate, but should really rename it. To receive an Intent (which should be sent as long as you call your main and add this) make sure the receiver overrides onReceive – Sam Aug 28 '18 at 12:12
  • 1
    what do you mean by 'did not work' – pixelatedCat Aug 28 '18 at 12:14
  • psvm() is not a part of android activity lifecycle and hence it will never be called. – Vivek Mishra Aug 28 '18 at 12:14
  • Hi Ugur, your code is not complete since you put/send the extra but you don't get/receive it at the second Activity. Check this: https://stackoverflow.com/a/5265952/10005752, and Good Luck! – khalid3e Aug 28 '18 at 12:17
  • `public static void main(String[] args) {` this is not how Android works. Looks like you need some basic Android tutorial before you continue – Vladyslav Matviienko Aug 28 '18 at 12:20

2 Answers2

1

You need to call getIntent() in your receiver activity.

Intent intent = getIntent();

and then you can call

intent.getExtras()

paxcow
  • 1,670
  • 3
  • 17
  • 32
1

The entry point to an android application is typically the onCreate method.

I suspect that your main method (traditional java entry point) is not being run. You could try calling it from the onCreate, but should really rename it.

To receive an Intent, which should be sent as long as you call your main and add the Intent code, make sure the receiver overrides onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    ArrayList<Double> NEFZ = intent.getExtras().getString("NEFZ");
}
Sam
  • 670
  • 1
  • 6
  • 20
  • Strangely enough the main method is running and the output is right. If I change the code to onCreate there is an error. Error running 'NEFZ2array': The activity must be exported or contain an intent-filter. So I cant see if the code is right or not. – Ugur Günes Aug 28 '18 at 15:07
  • Is it running in your IDE, on an emulator or on a device? – Sam Aug 28 '18 at 15:52
  • For testing the class is running on the IDE. But the app runs on a real device without any problem. – Ugur Günes Aug 29 '18 at 08:21
  • 1
    I recommend adding a toast or similar to your main method, to check that it is actually executed when running your app. – Sam Aug 29 '18 at 08:56