0

I'm trying to understand library usage in Android. So I created a very simple library and import it in my project. I want to call the method cubeOf() from MyOperations.jar (library file). There is no building errors but the application is stopping unexpectedly when I runned.

MainActivity.java class (in MyApplication):

package com.sam.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import com.sam.mylibrary.MyOperations;

public class MainActivity extends Activity {

    TextView tv1;

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

        tv1 = (TextView) findViewById(R.id.textView1);
        MyOperations op = new MyOperations();
        int a = op.cubeOf(8); // Just a random number
        tv1.setText(a);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

MyOperations.java class (in MyLibrary):

package com.sam.mylibrary;

public class MyOperations {
    public static void main(String[] arg) {

    }
    public static int cubeOf(int i) {
        int c = i*i*i;
        return c;
    }
}

SOLVED!

Thanks for replies, I solved the issue through this article.
smtnkc
  • 488
  • 2
  • 9
  • 23

3 Answers3

0

Change

    tv1.setText(a);

with

 tv1.setText(String.valueOf(a));

in your case you are using a as index for R.string, and if it does not exist, ResourceNotFoundException will be thrown

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

You are not mentioning the reason for your application to crash. I believe there might be 2 problems:

1 adding to blackbelt answer above:

you might simply use:

put setText(""+a)

So there isn't confusion about 'a' being used as numeric vs. string

2 You may have a build path issue.

Look at this answer:

How can I use external JARs in an Android project?

If you have a pretty recent version of Eclipse/ADT the folder should be called 'libs' else the folder was called 'lib' ( Eclipse/ADT TBD )

Also, the "Java Build Path" -> "Order And Export" make sure you are checking on the external library especially if from a different package.

Community
  • 1
  • 1
Rastikan
  • 517
  • 5
  • 18
  • I have already built the path, and yes i'm using a recent version. – smtnkc Dec 14 '13 at 22:39
  • Did you checked marked the library as export in the "Order And Export" tab ? Actually, are using a reference project or a compiled .jar version? Did you exported classes from the library project? – Rastikan Dec 14 '13 at 22:45
  • Do you have a s ky pe ac c o unt? I can offer a few minutes of support. – Rastikan Dec 14 '13 at 22:52
0

So just to confirm earlier responses, you tried both changes in onCreate? That is,

    int a = MyOperations.cubeOf(8); // Just a random number
    tv1.setText(String.valueOf(a));
  1. call static methods statically (don't even new a MyOperations)
  2. pass a string to setText
Rob Starling
  • 3,868
  • 3
  • 23
  • 40