简体   繁体   中英

How do I pass arrays from a java android activity class to a normal java class?

I'm new to java and am having issues with passing an array between classes. I want to pass data collected from the main activity for another class that is doing calculations. When I try to print the array values to the debug log in my calculations class I get an error "java.lang.NullPointerException: Attempt to read from null array"

Here is my calculations class:

package com.example.pokerholdemcalculator;

import android.util.Log;

public class CardHandling {
private int[] usedCards;
private MainActivity ma;

CardHandling() {
    usedCards = new int[13];
    ma = new MainActivity();
    usedCards = ma.getPlayerCardContents();
}

public void printC () {
    for (int i = 0; i < 13; i++) {
        String card = Integer.toString(usedCards[i]);
        Log.d("CARDS", card);
    }
}
}

And my MainActivity class:

package com.example.pokerholdemcalculator;

import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {
private ViewFlipper viewFlipper;
private boolean[] playerCardFlag;
private ImageButton[] playerCard;
private int[] playerCardContents;

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

    playerCard = new ImageButton[13];
    playerCardFlag = new boolean[13];
    playerCardContents = new int[13];       
}

public void testing (View v) {
    printArr();
}

public void printArr() {
    CardHandling ch = new CardHandling();
    ch.printC();
}

public int[] getPlayerCardContents() {
    return playerCardContents;
}

This is the error message from the console:

Caused by: java.lang.NullPointerException: Attempt to read from null array
    at com.example.pokerholdemcalculator.MainActivity.getPlayerCardContents(MainActivity.java:70)
    at com.example.pokerholdemcalculator.CardHandling.<init>(CardHandling.java:12)
    at com.example.pokerholdemcalculator.MainActivity.printArr(MainActivity.java:64)
    at com.example.pokerholdemcalculator.MainActivity.testing(MainActivity.java:60)

I have tried initializing in different ways but can't resolve the error.

The main problem here is that your activity cannot be referenced like this:

ma = new MainActivity();
usedCards = ma.getPlayerCardContents();

Want you can do is start the Class on the Main Activity

CardHandling ch = new CardHandling();
ch.usedCard = getPlayerCardContents();

MainActivity extends AppCompatActivity that means that your activity is not an ordinary object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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