开发者

Method call from a class in a separate file

I'm a complete noob obviously, but i'm trying to make a simple dummy application for a university project and i don't see what i'm doing wrong. I'm not very skilled at Java, so that could be a part of the problem, but i've been looking at examples and i still can't figure it out.

I've simplifed it to the best of my ability and it still won't work. so, i have 2 .java files in the same package

test.java:
package com.example.test;

import android.app.Activity;
import android.os.Bundle;

public class test extends Activity {

   public int zapis;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

   }

   public void onStart() {
       super.onStart();
       test2开发者_如何学Python klasa = null;
       zapis = klasa.a;
   }
}

and test2.java:

package com.example.test;

public class test2 {
   public int a=3;
   }

and if i run it, the app crashes :( What am i missing, some kind of a constructor? Thanks for your help.


If you want to access an object's members you first need to create the object.

replace

   test2 klasa = null;

with

   test2 klasa = new test2();

and it should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜