简体   繁体   English

来自单独文件中类的方法调用

[英]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. 我不太熟练Java,所以这可能是问题的一部分,但是我一直在查看示例,但仍然无法弄清楚。

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 所以,我在同一包中有2个.java文件

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 klasa = null;
       zapis = klasa.a;
   }
}

and test2.java: 和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. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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