简体   繁体   English

如何从android库类中调用方法

[英]How to call a method from android library class

I want to call a method from a android library class which i have imported as a androidlib.jar. 我想从我作为androidlib.jar导入的android库类中调用方法。 As i am able to call a whole class of library but i dont want it, but i want to call a particular method of library class. 因为我能够调用整个库类,但我不想要它,但是我想调用库类的特定方法。

I tried something like this, but it is showing java.lang.Nullpointer exception 我尝试过这样的事情,但它显示java.lang.Nullpointer异常

This is my library class (AndroidLiB.class), where i have imported its jar file 这是我的库类(AndroidLiB.class),我在其中导入了其jar文件

  public class AndroidLiB extends Activity  {

   @Override
   protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.android_li_b);
    startGPS();
    }

    public void startGPS()
    {
     Toast.makeText(getApplicationContext(),"Your GPS started",Toast.LENGTH_LONG).show();
    }

}

This is my application class where i want to call a method from above class 这是我的应用程序类,我想从上面的类中调用方法

  public class AndLib1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.and_lib1);
    AndroidLiB abc = new AndroidLiB();
    abc.startGPS();

    }

}

But it is not working 但这不起作用

I would do something like this: 我会做这样的事情:

public class Tool {
    private Tool() {
        // no direct instantiation
    }    

    public static void startGPS(final Context context) {
         Toast.makeText(context, "Your GPS started", Toast.LENGTH_LONG).show();
    }
}

then 然后

public class AndroidLiB extends Activity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_li_b);
        Tool.startGPS(this);
    }
}

and

public class AndLib1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.and_lib1);
        Tool.startGPS(this);
    }
}

You can extend library class. 您可以扩展库类。 For example: 例如:

public class YourActivity extends AndroidLib
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.and_lib1);
  startGPS();
}

If you want simply a method from jar, then why you need to extends Activity . 如果只需要jar中的方法,那么为什么需要扩展Activity My suggestion is remove extends Activity will fix the NPE error. 我的建议是删除扩展Activity将修复NPE错误。

Try this, 尝试这个,

 public class AndroidLiB {
  Activity activity;
  AndroidLiB(Activity activity){ 
        this.activity = activity;
   }


  public void startGPS()
  {
    Toast.makeText(activity,"Your GPS started",Toast.LENGTH_LONG).show();
  }

 }

And In your main class call like 在你的主班电话里

 AndroidLiB lib = new AndroidLiB (this);
 lib.startGPS();

What you're trying to do isn't really the best way to do things, but I'm assuming that the question being asked is how to import the jar correctly. 您要尝试执行的操作并不是真正的最佳方法,但是我假设所要问的问题是如何正确导入jar。 If so: 如果是这样的话:

If using eclipse, 如果使用日食

  1. Make sure the androidlib.jar file is in the libs folder. 确保androidlib.jar文件在libs文件夹中。
  2. Right click on androidlib.jar and select Build Path > Add to Build Path 右键单击androidlib.jar并选择Build Path > Add to Build Path
  3. Right click on your project folder, go to Properties > Java Build Path > Order and Export and then make sure androidlib.jar is selected. 右键单击项目文件夹,转到“ Properties > Java Build Path > Order and Export ,然后确保选择了androidlib.jar

The problem with your current code is that when you call getApplicationContext() , the Activity hasn't been started yet, therefore there is no context. 当前代码的问题是,当您调用getApplicationContext()Activity尚未启动,因此没有上下文。 A quick and dirty solution would be to rewrite the startGPS() method like this: 一种快速而肮脏的解决方案是像这样重写startGPS()方法:

public static void startGps(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

But I would much rather put that method inside some sort of Utilities class or even inside a parent Activity class. 但是我宁愿将该方法放在某种Utilities类中,甚至放在父Activity类中。

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

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