【问题标题】:Having trouble passing variable to be displayed on a screen on android无法传递要在 android 屏幕上显示的变量
【发布时间】:2012-01-03 19:06:15
【问题描述】:

我在开发我的 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

【问题讨论】:

    标签: java android eclipse rfid


    【解决方案1】:

    您需要从 'view' 参数中获取 myText。试试这个:

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

    【讨论】:

    • 我已经包含了:TextView mytext=(TextView)findViewById(R.id.widget96); mytext.setText((CharSequence) x);它在关于最后一段代码的编码的第三部分。
    • 请正确查看我的回复,您需要从视图中获取。我将编辑我的答案以加粗我强调的部分。
    • TextView mytext=(TextView)view.findViewById(R.id.widget96);即使这样,它仍然会启动强制关闭。
    • 你能检查一下logcat并发布stacktrace吗?
    • 对不起,我以前没有这样做过,我不确定如何访问 Logcat。查看 Eclipse 中的 Stack Trace 控制台,它只显示;将堆栈跟踪粘贴到此控制台中,并按照超链接指向源代码。
    猜你喜欢
    • 2012-02-06
    • 2015-09-05
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多