简体   繁体   English

如何访问另一个类中的函数?

[英]How can I access a function in another class?

I am developing an app for android that has to access another class, but i don't know why it doesn't work. 我正在为Android开发一个必须访问另一个类的应用程序,但是我不知道为什么它不起作用。

When run the App in android 2.3.3 it force closes, and I don't understand why. 在android 2.3.3中运行该应用程序时,它会强制关闭,我不明白为什么。 I think that the method is correct. 我认为该方法是正确的。

Log in the force close the phone android: 登录强制关闭手机android:

> app_vercode:1
device_model:u8800
build_version:111180
condition:1
processName:beta.tester
pid:13277
uid:10088
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at beta.tester.BetaTesterActivity.onCreate(BetaTesterActivity.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more

       Detail logs:

EDIT: This code already is correctly. 编辑:此代码已经正确。

The code: 编码:

class BetaTesterActivity: 类BetaTesterActivity:

   package beta.tester;

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


   public class BetaTesterActivity extends Activity {


 public TextView text1;
 private teste cmd;

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

         text1 = (TextView) findViewById(R.id.text1);
         //Start the function
         cmd = new teste();
         cmd.start(this);

    }
  }  

class teste: 类睾丸:

package beta.tester;

public class teste {

//Function that I will start
    public void start(BetaTesterActivity zav){

        zav.text1.setText("Hello");

   }
 //
}

In class teste , you are creating a new BetaTesterActivity , which is useless. teste类中,您将创建一个无用的新BetaTesterActivity You need to use the instance created by the framework. 您需要使用框架创建的实例。 Change your class teste to this: 将您的班级teste更改为此:

public class teste {

    //Function that I will start
    public void start(BetaTesterActivity zav){

        zav.text1.setText("Hello");

    }

}

Then in the onCreate method of your activity class, you need to initialize cmd and then call start like this: 然后,在活动类的onCreate方法中,您需要初始化cmd ,然后像这样调用start:

cmd.start(this);

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

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