开发者

Passing a String Array Between Java Classes Android App

开发者 https://www.devze.com 2023-01-28 07:28 出处:网络
I am writing an Android app where I need to pass a string array between two classes. The string initializes fine and I can output the contents of the string fine in the one class but as I try to pass

I am writing an Android app where I need to pass a string array between two classes. The string initializes fine and I can output the contents of the string fine in the one class but as I try to pass it to another class I get a Null Pointer Exception error. The following is the stripped down version of my code:

accelerometer.java:

    public class accelerometer extends Service {

 public String movement[];

 public开发者_如何学Go void onCreate() {

  movement = new String[1000000];

 }

 public void updatearray() {
  movement[arraypos]=getCurrentTimeString();
  //Toast.makeText(this, movement[arraypos] , Toast.LENGTH_SHORT).show(); //this correctly displays each position in the array every time it updates so I know the array is working correctly in this file
  arraypos+=1;
 }

 public String[] getmovement(){
  return movement;
 }
    }

wakeupalarm.java:

    public class wakeupalarm extends Activity {

 private TextView herestext_;

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.wakeup);

  herestext_ = (TextView) findViewById(R.id.TextView01);

  accelerometer accelerometercall = new accelerometer();

  String movearray[] = accelerometercall.getmovement();

  herestext_.setText(movearray[2]);
  }

    }

I have a feeling I'm missing something very simple but any help would be greatly appreciated!

Thanks,

Scott


You're creating a new accelerometer class, which is completely uninitialized since there is no constructor, then you access its member. Of course it'll be null.

Not sure how your two classes are related, but if the activity is called by the service, then you need to pass the string through the intent (through an extra, for example).

Side note: Class names should always start with a capital letter. Method/variable names should have camel case, i.e. "updateArray". Also, you can format your code here by selecting it and pressing CTRL+K.


Your first problem, I think, is that you are creating an array with a million slots in it. Do you really mean to be doing that? It's going to take a lot of memory---quite possibly more than is available. You should instead look to having a Vector of Strings that you extend as necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消