public class temperature extends ActionBarActivity implements View.OnClickListener{
Button connect;
TextView textView;
RadioButton radioButton,radioButton2;
private StringBuilder sb = new StringBuilder();
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what) {
case bluetooth.SUCCESS_CONNECT:
bluetooth.connectedThread = new bluetooth.ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), "Connected!", Toast.LENGTH_SHORT).show();
String s = "successfully connected";
bluetooth.connectedThread.start();
break;
case bluetooth.MESSAGE_READ:
Toast.makeText(getApplicationContext(), "Reading", Toast.LENGTH_SHORT).show();
try {
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0,5);
Log.d("strIncom", strIncom);
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\n"); // determine the end-of-line
//if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0,endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
Toast.makeText(getApplicationContext(),"DATA WRITE",Toast.LENGTH_SHORT ).show();
textView.setText(strIncom + "" + "C"); // update TextVie
textViewTemp.setText("DATA");
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt";//like 2016_01_12.txt
try
{
File root = new File(Environment.getExternalStorageDirectory()+File.separator+"Music_Folder", "Report File1");
if (!root.exists())
{
root.mkdirs();
}
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
writer.append("Temperature:"+strIncom+",");
writer.flush();
writer.close();
Toast.makeText(getApplicationContext(), "Data has been written to Report File", Toast.LENGTH_SHORT).show();
break;
}
catch(IOException e)
{
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"ISSUE",Toast.LENGTH_SHORT).show();
}
break;
} } };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temperature);
TextView tv = (TextView)findViewById(R.id.tv);
TextView textViewTemp = (TextView)findViewById(R.id.textViewTemp);
ButtonInit();
init();
}
void init(){
TextView textView = (TextView)findViewById(R.id.tv);
bluetooth.gethandler(mHandler);
}
void ButtonInit(){
connect = (Button)findViewById(R.id.button);
connect.setOnClickListener(this);
radioButton =(RadioButton)findViewById(R.id.radioButton);
radioButton.setOnClickListener(this);
radioButton2 =(RadioButton)findViewById(R.id.radioButton2);
radioButton2.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button:
startActivity(new Intent("android.intent.action.BT1"));
break;
case R.id.radioButton2:
if (radioButton2.isChecked()){
if (bluetooth.connectedThread != null)
bluetooth.connectedThread.write("Q");
}else{
if (bluetooth.connectedThread != null)
bluetooth.connectedThread.write("E");
}
break;
case R.id.radioButton:
if (radioButton.isChecked()){
if (bluetooth.connectedThread != null)
bluetooth.connectedThread.write("E");
}else{
if (bluetooth.connectedThread != null)
bluetooth.connectedThread.write("Q");
}
break;
}
}
这是XML
文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.biomapp.temperature"
android:id="@+id/temp"
android:orientation="vertical"
android:gravity="top|center"
android:textAlignment="gravity"
android:outlineProvider="background"
android:visibility="visible"
android:focusableInTouchMode="false"
android:focusable="false"
android:contentDescription="Temperaure "
android:clickable="false">
<TextView android:text="@string/tempcf" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewTemp"
android:enabled="true"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 - C/F"
android:id="@+id/tv"
android:textSize="50dp"
android:visibility="visible"
android:gravity="center"
android:layout_below="@+id/textViewTemp"
android:layout_centerHorizontal="true"
android:layout_marginTop="141dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONNECT"
android:onClick="connect"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:focusable="true"
android:layout_above="@+id/button">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius"
android:onClick="selectTemp"
android:id="@+id/radioButton"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:id="@+id/radioButton2"
android:checked="false" />
</RadioGroup>
</RelativeLayout>
我收到了这个错误
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5576)
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
03-22 02:48:14.567 19267-19267/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
03-22 02:48:14.576 19267-19267/? W/System.err﹕ android.content.pm.PackageManager$NameNotFoundException: com.example.blutoothtest
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:297)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at com.lenovo.powersetting.apprestriction.CleanupService.a(CleanupService.java:158)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at com.lenovo.powersetting.apprestriction.CleanupService.a(CleanupService.java:113)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at com.lenovo.powersetting.apprestriction.CleanupService.onStartCommand(CleanupService.java:290)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3079)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.app.ActivityThread.access$2100(ActivityThread.java:176)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1535)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:111)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5576)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
03-22 02:48:14.577 19267-19267/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
答案 0 :(得分:1)
替换
TextView tv = (TextView)findViewById(R.id.tv);
丝毫
this.tv = (TextView)findViewById(R.id.tv);
那么你可以设置像
这样的文字 this.tv.setText("text")