简体   繁体   中英

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.

When run the App in android 2.3.3 it force closes, and I don't understand why. I think that the method is correct.

Log in the force close the phone 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:

   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. You need to use the instance created by the framework. Change your class teste to this:

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:

cmd.start(this);

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