Hello,
I'm trying to implement a custom ListView
on Android, but I'm not getting it. It's something simple, I've done it before, but I can not see where I'm wrong.
Adapter_Bluetooth.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/frmNome_Bluetooth"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txtNome_Dispositivo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Texto"
android:textSize="50sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtNome_Dispositivo">
<TextView
android:id="@+id/txtCodigo_Dispositivo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Texto 2" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Activity_Listar_Bluetooth.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="br.ufscar.dc.controledepatrimonio.Forms.ListarBluetoothActivity">
<ListView
android:id="@+id/lstBluetooth"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</RelativeLayout>
BluetoothAdapert.java
package br.ufscar.dc.controledepatrimonio.Forms;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import br.ufscar.dc.controledepatrimonio.R;
public class BluetoothAdapter extends BaseAdapter {
private List<BluetoothDevice> dispostivos;
private LayoutInflater inflater;
private Context ctx;
public BluetoothAdapter(Context ctx, List<BluetoothDevice> dispositivos) {
this.dispostivos = dispositivos;
this.ctx = ctx;
}
@Override
public int getCount() {
return dispostivos.size();
}
@Override
public Object getItem(int position) {
return dispostivos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final BluetoothDevice dispositivo = (BluetoothDevice) getItem(position);
final int posicao = position;
View view = inflater.inflate(R.layout.adapter_bluetooth, null);
TextView txtNomeDispositivo = (TextView) view.findViewById(R.id.txtNome_Dispositivo);
txtNomeDispositivo.setText(dispositivo.getName());
TextView txtCodigoDispositivo = (TextView) view.findViewById(R.id.txtCodigo_Dispositivo);
txtCodigoDispositivo.setText(dispositivo.getAddress());
return view;
}
}
ListBluetoothActivity.java
package br.ufscar.dc.controledepatrimonio.Forms;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import br.ufscar.dc.controledepatrimonio.R;
import br.ufscar.dc.controledepatrimonio.Util.RFID.Bluetooth;
import br.ufscar.dc.controledepatrimonio.Util.RFID.BluetoothListener;
public class ListarBluetoothActivity extends AppCompatActivity implements BluetoothListener {
private Bluetooth bluetooth = new Bluetooth();
private final static int REQUEST_ENABLE_BT = 1;
private List<BluetoothDevice> listaPareados = new ArrayList<BluetoothDevice>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listar_bluetooth);
try {
switch (bluetooth.verificarEstado()) {
case LIGADO:
listaPareados = bluetooth.getPareados();
ListView lstBluetooth = (ListView) findViewById(R.id.lstBluetooth);
BluetoothAdapter itemBluetooth = new BluetoothAdapter(this, listaPareados);
lstBluetooth.setAdapter(itemBluetooth);
break;
case DESLIGADO:
Intent enableBtIntent = new Intent(bluetooth.getDispositivo().ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
break;
case NAO_COMPATIVEL:
Toast.makeText(getApplicationContext(), R.string.msg_bluetooth_nao_suportado, Toast.LENGTH_SHORT).show();
break;
}
}
catch (Exception ex) {
Log.e("ERRO:", ex.getMessage());
}
}
@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_listar_bluetooth, 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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void action(String action) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_ENABLE_BT) {
switch (resultCode) {
case RESULT_CANCELED:
finish();
break;
}
}
}
}
Log Error
08-06 10:54:08.955 25363-25363/? E/Zygote﹕ MountEmulatedStorage()
08-06 10:54:08.955 25363-25363/? E/Zygote﹕ v2
08-06 10:54:08.955 25363-25363/? I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_GT-I9505_5.0.1 ver=27
08-06 10:54:08.955 25363-25363/? I/libpersona﹕ KNOX_SDCARD checking this for 10214
08-06 10:54:08.955 25363-25363/? I/libpersona﹕ KNOX_SDCARD not a persona
08-06 10:54:08.965 25363-25363/? I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_GT-I9505_5.0.1_0027
08-06 10:54:08.965 25363-25363/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
08-06 10:54:08.965 25363-25363/? I/art﹕ Late-enabling -Xcheck:jni
08-06 10:54:09.035 25363-25363/? D/ResourcesManager﹕ creating new AssetManager and set to /data/app/br.ufscar.dc.controledepatrimonio-1/base.apk
08-06 10:54:09.185 25363-25363/? D/Activity﹕ performCreate Call secproduct feature valuefalse
08-06 10:54:09.185 25363-25363/? D/Activity﹕ performCreate Call debug elastic valuetrue
08-06 10:54:09.205 25363-25411/? D/OpenGLRenderer﹕ Render dirty regions requested: true
08-06 10:54:09.235 25363-25411/? I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 01/24/15 Sat
Local Branch: AF11_RB1_AU15
Remote Branch:
Local Patches:
Reconstruct Branch:
08-06 10:54:09.235 25363-25411/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-06 10:54:09.255 25363-25411/? D/OpenGLRenderer﹕ Enabling debug mode 0
08-06 10:54:09.395 25363-25363/? I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2f59a50b time:94052600
08-06 10:54:10.206 25363-25363/? D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
08-06 10:54:10.286 25363-25363/? I/Timeline﹕ Timeline: Activity_launch_request id:br.ufscar.dc.controledepatrimonio time:94053490
08-06 10:54:10.356 25363-25363/? D/AbsListView﹕ Get MotionRecognitionManager
08-06 10:54:10.366 25363-25363/? D/Activity﹕ performCreate Call secproduct feature valuefalse
08-06 10:54:10.366 25363-25363/? D/Activity﹕ performCreate Call debug elastic valuetrue
08-06 10:54:10.376 25363-25363/? D/AndroidRuntime﹕ Shutting down VM
08-06 10:54:10.376 25363-25363/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: br.ufscar.dc.controledepatrimonio, PID: 25363
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at br.ufscar.dc.controledepatrimonio.Forms.BluetoothAdapter.getView(BluetoothAdapter.java:45)
at android.widget.AbsListView.obtainView(AbsListView.java:2823)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1290)
at android.widget.ListView.onMeasure(ListView.java:1202)
at android.view.View.measure(View.java:18596)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
at android.view.View.measure(View.java:18596)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
08-06 10:54:11.718 25363-25363/? I/Process﹕ Sending signal. PID: 25363 SIG: 9
I'm still hitting my head here, but if anyone can give me a light where I'm wrong, I thank you immensely!