0

I been trying to fix this problem for a few days and still fail to fix it... Please someone can help me.

I am currently trying to develop a apps that do the same function with what starmicronic apps does. And you can find the SDK below.

http://www.starmicronics.com/support/ZipFile.aspx?sat2=195

At the start I just copy the Jar Library StarIOPort3.1.Jar into the workspace, and import to the Main.java

I tried to copy this function into my main class but at first I have missed to add the internet permission in the Manifest setting. After adding the internet permission I still get a runtime exeception of E/AndroidRuntime(776): java.lang.IllegalStateException: Could not execute method of the activity.

But it works fine with Star SDK... but not in my version.

public void PortDiscovery(View view)
{
    List<PortInfo> BTPortList;
    List<PortInfo> TCPPortList;
    final EditText editPortName;

    final ArrayList<PortInfo> arrayDiscovery;
    ArrayList<String> arrayPortName;

    arrayDiscovery = new ArrayList<PortInfo>();
    arrayPortName = new ArrayList<String>();

    try {

        BTPortList  = StarIOPort.searchPrinter("BT:");
        TCPPortList = StarIOPort.searchPrinter("TCP:");

        for (PortInfo portInfo : BTPortList) {
              arrayDiscovery.add(portInfo);
        }

        for (PortInfo portInfo : TCPPortList) {
            arrayDiscovery.add(portInfo);
        }

        arrayPortName = new ArrayList<String>();

        for(PortInfo discovery : arrayDiscovery)
        {
            String portName;

            portName = discovery.getPortName();

            if(discovery.getMacAddress().equals("") == false)
            {
                portName += "\n - " + discovery.getMacAddress();
                if(discovery.getModelName().equals("") == false)
                {
                    portName += "\n - " + discovery.getModelName();
                }
            }

            arrayPortName.add(portName);
        }


    } catch (StarIOPortException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    editPortName = new EditText(this);

    new AlertDialog.Builder(this)
    .setIcon(R.drawable.icon) 
    .setTitle("Please Select IP Address or Input Port Name") 
    .setView(editPortName) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int button)
        {
            EditText portNameField = (EditText)findViewById(R.id.editText_PortName);
            portNameField.setText(editPortName.getText());
            SharedPreferences pref = getSharedPreferences("pref", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
            Editor editor = pref.edit();
            editor.putString("portName", portNameField.getText().toString());
            editor.commit();
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int button)
        {
        }
    })
    .setItems(arrayPortName.toArray(new String[0]), new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int select)
        {
            EditText portNameField = (EditText)findViewById(R.id.editText_PortName);
            portNameField.setText(arrayDiscovery.get(select).getPortName());

            SharedPreferences pref = getSharedPreferences("pref", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
            Editor editor = pref.edit();
            editor.putString("portName", portNameField.getText().toString());
            editor.commit();
        }
    })
    .show();
}
Loyalar
  • 3
  • 1
Frank
  • 1
  • 1
  • I have tested with some log the program will give a run-time error in thie method BTPortList = StarIOPort.searchPrinter("BT:"); TCPPortList = StarIOPort.searchPrinter("TCP:"); – Frank Mar 11 '13 at 05:39
  • In addition, I tried to run in seperate project it didnt work. But if I create it on the same project (StarSDK) in different class and it works. I guess is doing with the configuration problem but the manufacturer didn't make it clear. For example, the manufacturer didn't include the internet permission setting in the manifest.xml. Therefore, would anyone able to help this out? – Frank Mar 11 '13 at 06:18
  • Can you give us a little more information? Where is the illegalStateException occurring? Do you have a stacktrace? – hack_on Mar 11 '13 at 06:39
  • Hi hack_on, I am not sure what do you mean, I am quite new in this field. Here a an example, two project running on the same code but one got run-time error and one (StarIOSDK) runs fine. I guess it would be more of a configuration problem. I am not sure what information should be provided, please let me know what I can get for you I will do it ASAP – Frank Mar 11 '13 at 07:04
  • When you run the app in eclipse, if you go to the DDMS perspective, there is a LogCat tab. The debug output goes to the LogCat tab. I would like to see the whole error to try and get a guess at what might be wrong. – hack_on Mar 11 '13 at 07:19
  • Many Thanks hack_on, please find the link below http://farm9.staticflickr.com/8233/8548126710_a3c97a0d47_b.jpg – Frank Mar 11 '13 at 07:27

1 Answers1

0

OK If you look at the other exceptions in the stacktrace you can see that it gets a NetworkOnMainThreadException. This means you need to get off the main UI thread before starting the code that searches for printers. You can create a Thread or use a task.

The answer to this SO question has a good example with a simple task that you could adapt for your purpose. So to make it clearer, I think the code that calls the PortDiscovery() methods needs to be in a Task.

The reason that Android gives this exception is that it can take minutes to try a network operation and then have it fail. This would lock the whole UI for that time and is therefore not a good practice.

According to this post this Exception was introduced in Android 3.0. The Android project on Starmicronics website you mention has a default build target of 7 (which is Android 2.1). It could be that the new example eclipse project has a higher build target, or the run-configuration runs on a higher version of Android in the emulator. You can check this in: project.properties and default.properties files.

Community
  • 1
  • 1
hack_on
  • 2,532
  • 4
  • 26
  • 30
  • Thank you hack_on. I tried the method you told me, but is still not working. I understand the logic behind it, but it seem to me that is the library configuration problem. As if I remove the internet permission in the manifest.xml file the application will not encounter any application error. In addition, the manufacturer STARSDK would works with the same code therefore my concern is more on the setting side. I uploaded the LogCat again, please let me know if you know anything to solve ... appreciate for you help so much :) http://farm9.staticflickr.com/8227/8549808233_c7a838fbbd_b.jpg – Frank Mar 12 '13 at 04:07
  • I can see the SearchPrinter method calls the Star Library and pointed to be (Unknown Source). I have added the Library Jar file into the project. I wonder what would be the reason for that? – Frank Mar 12 '13 at 04:21
  • If the two projects do in fact have identical source code, then check the .project and .classpath files in the top-level directory of each project. Are they practically identical? Compare the AndroidManaifest.xml files and the project.properties. Also, you can go into Project | Properties | Java Build Path and compare what is on every tab. – hack_on Mar 12 '13 at 08:03
  • Yes, I have compared two project with the files you listed. Still nothing working, there is a different which the StarSDK was made with older version of IDE. It has a proguard.cfg file instead of a proguard.txt file. But I google and still couldn't figure out the way to solve it... – Frank Mar 12 '13 at 08:50
  • @Frank see my updated answer, I think it is the build target. – hack_on Mar 12 '13 at 10:53
  • Thanks.. I give up on that. I guess later I just try to contact Star technical guys see what they say – Frank Mar 13 '13 at 06:48