在 Android 上传递要显示在屏幕上的变量时遇到问题

发布于 2024-12-24 01:52:49 字数 4130 浏览 3 评论 0原文

我在开发 Android 应用程序时遇到问题,目前正在使用 Eclipse 和 Phidg​​et RFID。我的目标是在 Android 设备上以一段文本形式显示 RFID 的 ID。

我已成功通过以下代码片段中的 println 显示 ID。

   package myFood.myFood;

import com.phidgets.*;
import com.phidgets.event.*;
public class RFID {
static String x ="NULL";
public static final Object main(String args[]) throws Exception {
    RFIDPhidget rfid;
    rfid = new RFIDPhidget();
    rfid.openAny();

    //Begin the TagGained event, allowing for users to read the RFID values
    rfid.addTagGainListener(new TagGainListener()
    {
        public void tagGained(TagGainEvent oe)
        {
            Object y = (oe.getValue());
            x= y.toString();
        }
    });

    long StartTime,RunTime;
    StartTime=System.currentTimeMillis();
    do{
        RunTime=System.currentTimeMillis();
        if (x.equals("NULL")) {
            //Continue waiting for input
            }
        else
        StartTime = 10000; //Overload the result so the loop ends
    }
    while (RunTime-StartTime<5000);

    rfid.close();
    return x;
}

}

进而。

package myFood.myFood;

public class RFIDresult {

public static final Object main(String args[]) throws Exception {
    System.out.println("Results");
Object x = RFID.main(args);
System.out.println(x);
return x;
}
}

但是我希望ID显示在一段文本中,所以我尝试将第二段代码开发到Android中。

package myFood.myFood;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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



    Button mybutton = (Button) findViewById(R.id.Button1);
    mybutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String[] args = null;
            Object x = null;
            try {
                x = RFID.main(args);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    TextView mytext=(TextView)findViewById(R.id.widget96);
    mytext.setText((CharSequence) x);


    }


    });
}

}

这只会产生一个关闭的力。我在这方面有点新手,我会感谢我得到的所有建议。


LogCat 报告;

ERROR/AndroidRuntime(519): FATAL EXCEPTION: main
ERROR/AndroidRuntime(519): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(519):     at myFood.myFood.RFID.main(RFID.java:9)
ERROR/AndroidRuntime(519):     at myFood.myFood.AddFood$1.onClick(AddFood.java:26)<br/>
ERROR/AndroidRuntime(519):     at android.view.View.performClick(View.java:2408)
ERROR/AndroidRuntime(519):     at android.view.View$PerformClick.run(View.java:8816)
ERROR/AndroidRuntime(519):     at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(519):     at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(519):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(519):    at     android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(519):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(519):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(519):     at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(519):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(519):     at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(519): Caused by: java.lang.ExceptionInInitializerError: Library phidget21 not found
ERROR/AndroidRuntime(519): Could not locate the Phidget C library (libphidget21.so).
ERROR/AndroidRuntime(519): Make sure it is installed, and add it's path to LD_LIBRARY_PATH.
ERROR/AndroidRuntime(519):     at com.phidgets.Phidget.<clinit>(Phidget.java:34)
ERROR/AndroidRuntime(519):     ... 13 more

I am having trouble with developing my Android application, I am currently using Eclipse and a Phidget RFID. My aim is to display the ID of the RFID in a piece of text on the Android device.

I have managed to display the ID through a println in the following pieces of code.

   package myFood.myFood;

import com.phidgets.*;
import com.phidgets.event.*;
public class RFID {
static String x ="NULL";
public static final Object main(String args[]) throws Exception {
    RFIDPhidget rfid;
    rfid = new RFIDPhidget();
    rfid.openAny();

    //Begin the TagGained event, allowing for users to read the RFID values
    rfid.addTagGainListener(new TagGainListener()
    {
        public void tagGained(TagGainEvent oe)
        {
            Object y = (oe.getValue());
            x= y.toString();
        }
    });

    long StartTime,RunTime;
    StartTime=System.currentTimeMillis();
    do{
        RunTime=System.currentTimeMillis();
        if (x.equals("NULL")) {
            //Continue waiting for input
            }
        else
        StartTime = 10000; //Overload the result so the loop ends
    }
    while (RunTime-StartTime<5000);

    rfid.close();
    return x;
}

}

and then.

package myFood.myFood;

public class RFIDresult {

public static final Object main(String args[]) throws Exception {
    System.out.println("Results");
Object x = RFID.main(args);
System.out.println(x);
return x;
}
}

However I want the ID to be displayed in a piece of text so I tried to develop the second piece of code into Android.

package myFood.myFood;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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



    Button mybutton = (Button) findViewById(R.id.Button1);
    mybutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String[] args = null;
            Object x = null;
            try {
                x = RFID.main(args);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    TextView mytext=(TextView)findViewById(R.id.widget96);
    mytext.setText((CharSequence) x);


    }


    });
}

}

And this just generates a force close. I am a bit of a novice at this and will appreciate all the advice I get.


LogCat Report;

ERROR/AndroidRuntime(519): FATAL EXCEPTION: main
ERROR/AndroidRuntime(519): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(519):     at myFood.myFood.RFID.main(RFID.java:9)
ERROR/AndroidRuntime(519):     at myFood.myFood.AddFood$1.onClick(AddFood.java:26)<br/>
ERROR/AndroidRuntime(519):     at android.view.View.performClick(View.java:2408)
ERROR/AndroidRuntime(519):     at android.view.View$PerformClick.run(View.java:8816)
ERROR/AndroidRuntime(519):     at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(519):     at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(519):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(519):    at     android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(519):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(519):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(519):     at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(519):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(519):     at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(519): Caused by: java.lang.ExceptionInInitializerError: Library phidget21 not found
ERROR/AndroidRuntime(519): Could not locate the Phidget C library (libphidget21.so).
ERROR/AndroidRuntime(519): Make sure it is installed, and add it's path to LD_LIBRARY_PATH.
ERROR/AndroidRuntime(519):     at com.phidgets.Phidget.<clinit>(Phidget.java:34)
ERROR/AndroidRuntime(519):     ... 13 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-12-31 01:52:49

您需要从“view”参数获取 myText。试试这个:

TextView mytext=(TextView)**view**.findViewById(R.id.widget96);

You need to get myText from 'view' parameter. Try this:

TextView mytext=(TextView)**view**.findViewById(R.id.widget96);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文