我正在開發Android 4.1.1平板電腦上的應用程序,但Galaxy S4和S4mini沒有。錯誤是拉着我:E/MoreInfoHPW_ViewGroup:父視圖不是TextView
E/MoreInfoHPW_ViewGroup:父視圖不是一個TextView
方式離開的代碼,看看是否有人能幫助我無法找到解決辦法。
MainActivity:
package com.dydsistemas.demoler.espejorgb;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity extends Activity {
private ColorPicker colorPicker;
private Button button;
private TextView tvRGB;
protected static final int REQUEST_CODE = 10;
private String resultMac;
//SECCION DECLARACIONES BLUETOOTH
Handler bluetoothIn;
final int handlerState = 0; //used to identify handler message
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder recDataString = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service - this should work for most devices
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvRGB = (TextView) findViewById(R.id.tvRGB);
colorPicker = (ColorPicker) findViewById(R.id.colorPicker);
colorPicker.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int color = colorPicker.getColor();
String rgbString = "R: " + Color.red(color) + " B: " + Color.blue(color) + " G: " + Color.green(color);
String RGB;
RGB = Color.red(color) + "," + Color.blue(color) + "," + Color.green(color);
tvRGB.setText(RGB);
}
});
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int color = colorPicker.getColor();
String rgbString = "R: " + Color.red(color) + " B: " + Color.blue(color) + " G: " + Color.green(color);
String RGB;
RGB = Color.red(color) + "," + Color.blue(color) + "," + Color.green(color);
try
{
mConnectedThread.write(RGB); // Send RGB via Bluetooth
tvRGB.setText(RGB);
// Toast.makeText(MainActivity.this, rgbString, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
/// Toast.makeText(getBaseContext(), "Error el enviar el comando. No esta conectado el Espejo", Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
}
});
//SECCION BLUETOOTH
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
}
/* FUNCIONA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
colorPicker = (ColorPicker) findViewById(R.id.colorPicker);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int color = colorPicker.getColor();
String rgbString = "R: " + Color.red(color) + " B: " + Color.blue(color) + " G: " + Color.green(color);
String RGB;
RGB = Color.red(color) + "," + Color.blue(color) + "," + Color.green(color);
mConnectedThread.write(RGB); // Send RGB via Bluetooth
Toast.makeText(MainActivity.this, rgbString, Toast.LENGTH_SHORT).show();
}
});
//SECCION BLUETOOTH
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
}
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
// cogemos el valor devuelto por la otra actividad
if (data.getDataString()!=null) {
resultMac = data.getDataString();
// enseñamos al usuario el resultado
// Toast.makeText(this, "SettingActivity devolvió: " + resultMac, Toast.LENGTH_LONG).show();
//create device and set the MAC address
BluetoothDevice device = btAdapter.getRemoteDevice(resultMac);
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
// Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show();
}
// Establish the Bluetooth socket connection.
try {
btSocket.connect();
//prueba
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
//I send a character when resuming.beginning transmission to check device is connected
//If it is not an exception will be thrown in the write method and finish() will be called
mConnectedThread.write("128,128,128");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
//insert code to deal with this
// Toast.makeText(getBaseContext(), "No se encontró Espejo RGB. ", Toast.LENGTH_LONG).show();
}
}
/*mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
//I send a character when resuming.beginning transmission to check device is connected
//If it is not an exception will be thrown in the write method and finish() will be called
mConnectedThread.write("128,128,128");
*/
}
}
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
/*
@Override
public void onPause()
{
super.onPause();
try
{
//Don't leave Bluetooth sockets open when leaving activity
btSocket.close();
} catch (IOException e2) {
//insert code to deal with this
}
}
*/
//Checks that the Android device Bluetooth is available and prompts to be turned on if off
private void checkBTState() {
if(btAdapter==null) {
// Toast.makeText(getBaseContext(), "Device does not support bluetooth", Toast.LENGTH_LONG).show();
} else {
if (btAdapter.isEnabled()) {
} else {
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
}
}
//create new class for connect thread
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer); //read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity via handler
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
break;
}
}
}
//write method
public void write(String input) {
byte[] msgBuffer = input.getBytes(); //converts entered String into bytes
try {
mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream
} catch (IOException e) {
//if you cannot write, close the application
// Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
finish();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
//abro activity Settings
/*
Intent intentSettings = new Intent(MainActivity.this, SettingActivity.class);
startActivity(intentSettings);
*/
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
// damos valor al parámetro a pasar
//intent.putExtra("param1", "valor del parámetro 1 (viene de mainActivity)");
/*
* Inicia una actividad que devolverá un resultado cuando
* haya terminado. Cuando la actividad termina, se llama al método
* onActivityResult() con el requestCode dado.
* El uso de un requestCode negativo es lo mismo que llamar a
* startActivity(intent) (la actividad no se iniciará como una
* sub-actividad).
*/
startActivityForResult(intent, REQUEST_CODE);
return true;
case R.id.about:
//abro activity about
Intent intentAbout = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intentAbout);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SettingActivity:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class SettingActivity extends MainActivity {
private static final int OK_RESULT_CODE = 1;
private String address;
private Button On,Off,Visible,list;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
private ListView lv;
// EXTRA string to send on to mainactivity
public static String EXTRA_DEVICE_ADDRESS = "device_address";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
On = (Button)findViewById(R.id.button1);
Off = (Button)findViewById(R.id.button2);
Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
//String addres = (String)adapter.getItemAtPosition(position);
String info; //= ((TextView) v).getText().toString();
info = lv.getItemAtPosition(position).toString();
address = info.substring(info.length() - 17);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
returnParams();
/* Intent i = new Intent(SettingActivity.this, MainActivity.class);
i.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(i);*/
}
});
}
protected void returnParams() {
Intent intent = new Intent();
//intent.putExtra(address, "'Valor devuelto por ParametrosActivity'");
intent.setData(Uri.parse(address));
setResult(OK_RESULT_CODE, intent);
finish();
}
@Override
protected void onResume() {
super.onResume();
}
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
// Toast.makeText(getApplicationContext(), "Turned on"
//
, Toast.LENGTH_LONG).show();
}
else{
// Toast.makeText(getApplicationContext(),"Already on",
// Toast.LENGTH_LONG).show();
}
}
public void list(View view){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) {
list.add(bt.getName() + " - " + bt.getAddress());
}
// Toast.makeText(getApplicationContext(),"Showing Paired Devices",
// Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
public void off(View view){
BA.disable();
// Toast.makeText(getApplicationContext(),"Turned off" ,
// Toast.LENGTH_LONG).show();
}
public void visible(View view){
Intent getVisible = new Intent(BluetoothAdapter.
ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
}
感謝您的時間
能爲您提供更全面的堆棧跟蹤嗎?行號會有幫助。謝謝 – 2015-01-04 14:07:39
哪條線給你這條信息? – 2015-01-04 14:23:21
如果沒有導致崩潰,請忽略它。這是三星常見的錯誤。 – 2015-01-04 14:30:15