Android Intent: sending Integer ArrayList

Johnatan56

Honorary Master
Joined
Aug 23, 2013
Messages
24,603
#1
Hi,
I am currently writing an app for class whereby I am trying to send two arrays to a second class in order to display them in a table (each has a column using TableLayout).
So when you click the button, two ArrayLists are created that need to be sent over. So far I am getting a NullPointerException on line 24, which is when I try to get the intent from the other class. I am still new to intents (learnt about them today in class), so any advice is appreciated.

(It is a subnet calculator)

The button on the main screen:
Code:
    public void sendNetworkSecond(View v){
        Intent intent = new Intent(this, NetworkSecond.class);

        final EditText subnetSlash = (EditText)findViewById(R.id.slashSubnet);
        try {
            subSlash = Integer.parseInt(subnetSlash.getText().toString());
        } catch (NumberFormatException e) {
            subSlash = 24;  //Default subnet.
        }

        //Subnet calculation suggestion for class C.
        if(subSlash > 24){
            x = 0;
            y = 0;
            x = subSlash / 8;
            y = (subSlash - x*8);
            networkPortion = y;
        }

        //Subnet calculation suggestion for class B
        if(subSlash > 16 && subSlash <24){
            x = 0;
            y = 0;
            x = subSlash / 8;
            y = (subSlash - x*8);
            networkPortion = y;
        }

        //Calculate the range.
        if(networkPortion != 0){
            //Calculate the number of networks.
            amountNetworks = (int) Math.pow(2.0,(double)(networkPortion)) - 2;
            //Calculate number of hosts.
            amountHosts = (x + 1) * 8 - amountNetworks;
            //Calculate the range.
            range = 256 / amountNetworks;
        }

        //Calculate network address and broadcast.
        if(range != 0){
            int total = 0;
            while(total < 255) {
                networkAddress.add(total);
                broadcastAddress.add(total + range - 1);
                total += range;
            }
        }

        //Send over to next screen. Make sure that there is a range, else there would be nothing to send.
        if(range!= 0){
            intent.putIntegerArrayListExtra("networkAddress", networkAddress);
            intent.putIntegerArrayListExtra("broadcastAddress", broadcastAddress);
        }
        startActivity(intent);
    }
The code for the second class:
Code:
public class NetworkSecond extends Activity{

    Intent intent = getIntent();
    ArrayList<Integer> networkAddress = new ArrayList<>();
    ArrayList<Integer> broadcastAddress = new ArrayList<>();

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

        try{
            networkAddress = intent.getIntegerArrayListExtra("networkAddress");
            broadcastAddress = intent.getIntegerArrayListExtra("broadcastAddress");
        }catch(Exception e){
            //In case the Array is empty.
            networkAddress.add(0);
            broadcastAddress.add(0);
        }

        //Need to use the table to display the two arrays. One array per row.
        TableLayout table = (TableLayout)findViewById(R.id.tableLayout);

        for(int i = 0; i < networkAddress.size(); i++){
            TableRow row = new TableRow(this);
            int subnetID = networkAddress.get(i);
            int broadcast = broadcastAddress.get(i);
            TextView subnetting = new TextView(this);
            subnetting.setText("" + subnetID);
            TextView broadcasting = new TextView(this);
            broadcasting.setText("-" + broadcast);
            row.addView(subnetting);
            row.addView(broadcasting);
            table.addView(row);

        }
    }
}
Thanks for any help.

EDIT: If I comment out the try/catch so it only allows the getIntent, it is a NullPointerException at line 24 in NetworkSecond. I am assuming there is an issue with the sending of the intent?
Thanks.
 

mercurial

MyBB Legend
Joined
Jun 12, 2007
Messages
37,889
#3
You need to debug. If you don't come right, you can use the less desired way and use a bundle to send over data.
 

Johnatan56

Honorary Master
Joined
Aug 23, 2013
Messages
24,603
#4
So I managed to fix it up, moved the creation of the arraylist and intent into the onCreate.
So it works in printing out the arraylist, but how would I go about deleting it the next time it runs (clicking back and then clicking the button that sends one to the screen)?
I have cleared the arrayLists at the end, so the values that are printed are fine, just can't figure out to clear.


Screen 2
Code:
package thegroup.calculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;

public class NetworkSecond extends Activity{

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

        ArrayList<Integer> networkAddress = new ArrayList<Integer>();
        ArrayList<Integer> broadcastAddress = new ArrayList<Integer>();

        Intent getNetMain = getIntent();

        try{
            networkAddress = getNetMain.getIntegerArrayListExtra("networkAddress");
            broadcastAddress = getNetMain.getIntegerArrayListExtra("broadcastAddress");
        }catch(Exception e){
            //In case the Array is empty.
            networkAddress.add(0);
            broadcastAddress.add(0);
        }

        //Need to use the table to display the two arrays. One array per row.
        TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);

        //Clear the table?

        //Display the ArrayLists<Integer>.
        for(int i = 0; i < networkAddress.size(); i++){
            TableRow row = new TableRow(this);
            int subnetID = networkAddress.get(i);
            int broadcast = broadcastAddress.get(i);
            TextView subnetting = new TextView(this);
            subnetting.setText("" + subnetID);
            TextView broadcasting = new TextView(this);
            broadcasting.setText("-" + broadcast);
            row.addView(subnetting);
            row.addView(broadcasting);
            tableLayout.addView(row);

        }

        networkAddress.clear();
        broadcastAddress.clear();
    }
}
XML of screen 2
Code:
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/tableLayout">
    </TableLayout>

</ScrollView>
EDIT:
Image of problem:
Problem.png
(Didn't change the values that are calculated, each time you see a 0 it means I've gone back and clicked the button again. How would I clear the previous?)
Thanks

EDIT2:
Figured it out. The reason tableLayout.removeAllViews() didn't "work" was because I forgot to clear the ArrayList from screen one (I did clear it in screen two).
 
Last edited:
Top