每次打开活动(blueToothSearch.java
)时我都会遇到问题,我的logcat中会发生以下错误。
12-04 18:57:52.317 5354-5354/com.example.admin.paniccenter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.paniccenter, PID: 5354
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.paniccenter/com.example.admin.paniccenter.blueToothSearch}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at org.altbeacon.beacon.BeaconManager.<init>(BeaconManager.java:323)
at org.altbeacon.beacon.BeaconManager.getInstanceForApplication(BeaconManager.java:315)
at com.example.admin.paniccenter.blueToothSearch.<init>(blueToothSearch.java:65)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
下面是我的blueToothSearch
活动,我想使用该活动显示我所在区域的信标列表。
public class blueToothSearch extends AppCompatActivity implements BeaconConsumer {
private static final String TAG = "blueToothSearch";
private ViewPagerAdapter viewadapter;
private ViewPager myViewPager;
private TabLayout tabLayout;
public Toolbar toolbar;
//public static Handler UIHandler;
private static final int REQUEST_ENABLE_BT = 1234;
//public static final String EXTRAS_TARGET_ACTIVITY = "extrasTargetActivity";
//private static final String JAALEE_BEACON_PROXIMITY_UUID = "EBEFD083-70A2-47C8-9837-E7B5634DF524";//Jaalee BEACON Default UUID
// private static final Region ALL_BEACONS_REGION = new Region("rid", null, null, null);
//public static final String EXTRAS_BEACON = "extrasBeacon";
public BeaconManager beaconManager = BeaconManager.getInstanceForApplication(getBaseContext());
private ArrayList<Beacon> arrayL = new ArrayList<Beacon>();
private BeaconServiceUtility beaconUtill = null;
private BeaconAdapter adapterr;
public ListView list;
private LayoutInflater inflater;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
viewadapter = new ViewPagerAdapter(getSupportFragmentManager());
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("Bluetooth Devices");
//Set up the View Pager with the View page adapter
myViewPager = (ViewPager)findViewById(R.id.viewpager);
tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(myViewPager);
//tabLayout.setVisibility(View.GONE);
final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setImageResource(R.drawable.ic_popup_sync_24);
setupViewPager(myViewPager);
setSupportActionBar(toolbar);
//viewPager.setAdapter(adapter);
myViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 0: fab.show();
break;
case 1: fab.hide();
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//getSupportActionBar().setDisplayShowHomeEnabled(true);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
drawer.closeDrawer(Gravity.LEFT);
}
});
adapterr = new BeaconAdapter();
list = findViewById(R.id.beaconlist);
list.setAdapter(adapterr);
//list.setOnItemClickListener(createOnItemClickListener());
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new blueToothSearch.bluetooth(), "Available Devices");
adapter.addFragment(new bluetoothProperties(), "Device Properties");
viewPager.setAdapter(adapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_ENABLE_BT){
if (resultCode == Activity.RESULT_OK){
connectToService();
}else {
Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_LONG).show();
getSupportActionBar().setSubtitle("Bluetooth not enabled");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void connectToService(){
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
}catch (RemoteException e){
e.printStackTrace();
}
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
}catch (RemoteException e){
e.printStackTrace();
}
}
/**
private AdapterView.OnItemClickListener createOnItemClickListener(){
return new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
if (getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY) != null){
try {
Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
Intent intent = new Intent(blueToothSearch.this, clazz );
Beacon temp = adapterr.getItem(pos);
intent.putExtra(EXTRAS_BEACON, temp);
if(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY).contains("com.example.admin.paniccenter.CharacteristicsDemoActivity"))
{
startActivity(intent);
// if (temp.getProximityUUID().equalsIgnoreCase(JAALEE_BEACON_PROXIMITY_UUID)) {
// startActivity(intent);
// }
// else
// {
// ListBeaconsActivity.this.runOnUiThread(new Runnable()
//{
// public void run()
// {
//Toast.makeText(ListBeaconsActivity.this, "Current Beacon is in Non-Connectable mode", Toast.LENGTH_LONG).show();
// }
// });
// }
}
else
{
startActivity(intent);
}
}catch (ClassNotFoundException e){
Log.e(TAG, "Finding class by name failed", e);
}
}
}
};
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.scan_menu, menu);
MenuItem refreshItem = menu.findItem(R.id.refresh);
refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
beaconUtill.onStart(beaconManager, this);
}
@Override
public void onStop() {
beaconUtill.onStop(beaconManager, this);
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onBeaconServiceConnect() {
getSupportActionBar().setSubtitle("Scanning...");
Toast.makeText(this, "Scanning for devices", Toast.LENGTH_LONG).show();
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
arrayL.clear();
arrayL.addAll((ArrayList<Beacon>) beacons);
adapterr.notifyDataSetChanged();
}
});
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.e("BeaconDetactorService", "didEnterRegion");
}
@Override
public void didExitRegion(Region region) {
Log.e("BeaconDetactorService", "didExitRegion");
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
Log.e("BeaconDetactorService", "didDetermineStateForRegion");
}
});
connectToService();
}
public static class bluetooth extends Fragment {
private static final String TAG = "bluetooth";
private int position;
private TextView textv;
private ViewPagerAdapter viewadapter;
private ViewPager myViewPager;
private TabLayout tabLayout;
private FragmentActivity myContext;
@Override
public void onAttach(Context context) {
super.onAttach(context);
//hideAction = (HideActionBar)getContext();
myContext = (FragmentActivity)getContext();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//position = getArguments().getInt("pos");
//toolbar.getMenu().clear();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return inflater.inflate(R.layout.fragment_bluetooth_search, container, false);
View view = inflater.inflate(R.layout.fragment_bluetooth_search, container, false);
//TextView text = (TextView)getActivity().findViewById(R.id.view);
//text.setText("i am the real text ");
//hideAction.customBlueToothToolbar();
//FloatingActionButton fl = (FloatingActionButton)getActivity().findViewById(R.id.fab);
//fl.setImageResource(R.drawable.ic_popup_sync_24);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
public class BeaconAdapter extends BaseAdapter {
private ArrayList<Beacon> beacons;
@Override
public int getCount() {
return arrayL.size();
}
@Override
public Beacon getItem(int arg0) {
return arrayL.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
ViewHolder holder;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
}
if (beacons.get(position).getParserIdentifier() != null)
holder.beacon_uuid.setText("UUID: " + arrayL.get(position).getParserIdentifier());
holder.beacon_major.setText("Major: " + arrayL.get(position).getBeaconTypeCode());
holder.beacon_minor.setText(", Minor: " + arrayL.get(position).getBluetoothName());
holder.beacon_proximity.setText("Proximity: " + arrayL.get(position).getDistance());
holder.beacon_rssi.setText(", Rssi: " + arrayL.get(position).getRssi());
holder.beacon_txpower.setText(", TxPower: " + arrayL.get(position).getTxPower());
holder.beacon_range.setText("" + arrayL.get(position).getExtraDataFields());
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
private class ViewHolder {
private TextView beacon_uuid;
private TextView beacon_major;
private TextView beacon_minor;
private TextView beacon_proximity;
private TextView beacon_rssi;
private TextView beacon_txpower;
private TextView beacon_range;
public ViewHolder(View view) {
beacon_uuid = (TextView) view.findViewById(R.id.BEACON_uuid);
beacon_major = (TextView) view.findViewById(R.id.BEACON_major);
beacon_minor = (TextView) view.findViewById(R.id.BEACON_minor);
beacon_proximity = (TextView) view.findViewById(R.id.BEACON_proximity);
beacon_rssi = (TextView) view.findViewById(R.id.BEACON_rssi);
beacon_txpower = (TextView) view.findViewById(R.id.BEACON_txpower);
beacon_range = (TextView) view.findViewById(R.id.BEACON_range);
view.setTag(this);
}
}
}
我正在使用BeaconManager
类来范围和扫描我的信标,该库来自软件包org.altbeacon.beacon