- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用我的 arduino uno 和 HC-05 蓝牙模块创建一个家庭自动化 Android 应用程序。一切都已经很好了。它可以扫描、连接、开/关蓝牙、使用按钮和开关控制控制灯泡的开和关,但应用程序的设计很糟糕且朴素。所以我决定使用选项卡小部件。问题是,当我更改某些内容时,它不会运行并显示 NullPointerException 问题,而且我不知道为什么会发生 ots,因为它在过去几天里已经是一个可以运行的应用程序。
以下是 LogCat 中的列表:
02-21 08:48:18.063: E/AndroidRuntime(25982): FATAL EXCEPTION: main
02-21 08:48:18.063: E/AndroidRuntime(25982): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.ControllerTab}: java.lang.NullPointerException
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.os.Handler.dispatchMessage(Handler.java:107)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.os.Looper.loop(Looper.java:194)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-21 08:48:18.063: E/AndroidRuntime(25982): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 08:48:18.063: E/AndroidRuntime(25982): at java.lang.reflect.Method.invoke(Method.java:525)
02-21 08:48:18.063: E/AndroidRuntime(25982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-21 08:48:18.063: E/AndroidRuntime(25982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-21 08:48:18.063: E/AndroidRuntime(25982): at dalvik.system.NativeStart.main(Native Method)
02-21 08:48:18.063: E/AndroidRuntime(25982): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.ControllerTab}: java.lang.NullPointerException
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2186)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.widget.TabHost.addTab(TabHost.java:240)
02-21 08:48:18.063: E/AndroidRuntime(25982): at com.example.javac101.MainActivity.onCreate(MainActivity.java:164)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.Activity.performCreate(Activity.java:5122)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-21 08:48:18.063: E/AndroidRuntime(25982): ... 11 more
02-21 08:48:18.063: E/AndroidRuntime(25982): Caused by: java.lang.NullPointerException
02-21 08:48:18.063: E/AndroidRuntime(25982): at com.example.javac101.ControllerTab.onCreate(ControllerTab.java:110)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.Activity.performCreate(Activity.java:5122)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
02-21 08:48:18.063: E/AndroidRuntime(25982): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-21 08:48:18.063: E/AndroidRuntime(25982): ... 21 more
我的 MainActivity 中的 Logcat 中提到的行:
tabHost.addTab(tabSpecController);
我的 ControllerTab 中的 Logcat 中提到的行:
list.setOnItemClickListener(new OnItemClickListener()
可能不太明白,所以这是我在MainActivity中的代码:包 com.example.javac101;
public class MainActivity extends TabActivity {
//BLUETOOTH
BluetoothAdapter BTAdapter;
//BluetoothDevice BTDevice;
//Layout view daw
public static TextView title;
public static ImageView status;
//Intent
private static final int REQUEST_DEVICE_CONNECT = 1;
private static final int REQUEST_ENABLE_BLUETOOTH = 2;
private static final int REQUEST_ABOUTS = 3;
//object for bluetooth command service
public static BluetoothCommandService commandService = null;
//
private String connectedDeviceName = null;
//Message types sent from the Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
public static final int RECIEVE_MESSAGE = 6;
//Will be used in BluetoothCommandSrvice jave file
public static final String TOAST = "toast";
public static final String DEVICENAME = "device name";
public static final String HC05 = "20:13:06:19:34:54";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String address = new String(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
if (address.equals(MainActivity.HC05))
{
Intent login = new Intent (this, LoginActivity.class);
}
else
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabs);
/**
* DATABASE CRUD Operations
* */
DatabaseHandler db = new DatabaseHandler(this);
// Inserting Devices
Log.d("Insert: ", "Inserting ..");
db.addDevice(new Device(1, "DB Device 1"));
db.addDevice(new Device(2, "DB Device 2"));
db.addDevice(new Device(3, "DB Device 3"));
// Reading all devices
Log.d("Reading: ", "Reading all devices..");
List<Device> devices = db.getAllDevices();
for (Device dv : devices) {
String log = "Id: "+dv.getID()+" ,Name: " + dv.getName();
Log.d("Name: ", log);
}
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
title = (TextView) findViewById(R.id.title_left_text); // Set up the custom title
title.setText(R.string.app_name); // Set up the custom title
title = (TextView) findViewById(R.id.title_right_text); // Set up the custom title
Resources ressources = getResources();
TabHost tabHost2 = getTabHost();
TabHost tabHost = tabHost2;
// Controller Tab
Intent intentController = new Intent().setClass(this, ControllerTab.class);
TabSpec tabSpecController = tabHost
.newTabSpec("Controller")
.setIndicator("", ressources.getDrawable(R.drawable.icon_home_config))
.setContent(intentController);
// Setting Tab
Intent intentSetting = new Intent().setClass(this, Settings.class);
TabSpec tabSpecSetting = tabHost
.newTabSpec("Setting")
.setIndicator("", ressources.getDrawable(R.drawable.icon_settings_config))
.setContent(intentSetting);
// add all tabs
tabHost.addTab(tabSpecController);
tabHost.addTab(tabSpecSetting);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
// AUTO REQUEST OF ENABLING THE BLUETOOTH
BTAdapter = BluetoothAdapter.getDefaultAdapter();
//A code that will detect if BT is enabled otherwise will require it.
if (BTAdapter == null)
{
//Toast.makeText(context, text, duration)
Toast.makeText(this, "No Bluetooth adapter is available.", Toast.LENGTH_LONG).show();
finish();
return;
}
}
}
private void setVisible(int tabs, boolean b) {
// TODO Auto-generated method stub
}
@Override
protected void onStart()
{
super.onStart();
//Requesting Bluetooth automatically when its not yet enabled.
if (!BTAdapter.isEnabled())
{
Intent enableIntent = new Intent (BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableIntent, 0);
startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
}
else
{
if (commandService == null)
setupCommand();
}
}
@Override
protected void onResume() {
super.onResume();
if (commandService != null)
{
if (commandService.getState() == BluetoothCommandService.stateNothing)
{
commandService.start();
}
}
}
private void setupCommand()
{
commandService = new BluetoothCommandService(this, bluetoothHandler);
}
@Override
protected void onDestroy()
{
super.onDestroy();
if (commandService != null)
commandService.stop();
}
public final Handler bluetoothHandler = new Handler()
{
@Override
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case MESSAGE_STATE_CHANGE:
switch (msg.arg1)
{
case BluetoothCommandService.stateConnected:
title.setText(R.string.title_connectedTo);
title.append(connectedDeviceName);
break;
case BluetoothCommandService.stateConnecting:
title.setText(R.string.title_connecting);
break;
case BluetoothCommandService.stateListen:
case BluetoothCommandService.stateNothing:
title.setText(getString(R.string.title_notConnected));
break;
}
break;
case MESSAGE_DEVICE_NAME:
connectedDeviceName = msg.getData().getString(DEVICENAME);
Toast.makeText(getApplicationContext(), "Connected to " + connectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();
break;
//case RECIEVE_MESSAGE: // if receive message
// byte[] readBuf = (byte[]) msg.obj;
// String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
// sb.append(strIncom); // append string
// int endOfLineIndex = sb.indexOf("\r\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
//txtArduino.setText("Data from Arduino: " + sbprint); // update TextView
//1/4/14
// btn_d1_on.setEnabled(true);
// btn_d1_off.setEnabled(true);
// }
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
//break;
}
//return false;
}
};
//});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_DEVICE_CONNECT:
// When DeviceList Activity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
//String address = data.getExtras().getString(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
String address = new String(data.getExtras().getString(DeviceList.EXTRA_DEVICE_MAC_ADDRESS));
if (address.equals(HC05))
{
// Get the BLuetoothDevice object
BluetoothDevice device = BTAdapter.getRemoteDevice(address);
// Attempt to connect to the device
commandService.connect(device);
}
else
{
Toast.makeText(this, "This device is not for JavaC101 Application. Please connect to HC-05 device.", Toast.LENGTH_LONG).show();
}
}
break;
case REQUEST_ENABLE_BLUETOOTH:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
setupCommand();
} else {
// User did not enable Bluetooth or an error occured
Toast.makeText(this, R.string.notEnabledBluetooth, Toast.LENGTH_SHORT).show();
finish();
}
}
}
@Override
//Creating an Option Menu for connectivity and discoverability of a BT device
public boolean onCreateOptionsMenu(Menu menu)
{
//MenuInflater is a class
MenuInflater OptionMenu = getMenuInflater();
//OptionMenu.inflate(menuRes, menu)
OptionMenu.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.connect:
connect();
return true;
case R.id.bton:
bluetoothOn();
return true;
case R.id.btoff:
bluetoothOff();
return true;
case R.id.abouts:
abouts();
return true;
//default:
//return super.onOptionsItemSelected(item);
}
return false;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
private void connect()
{
Intent serverIntent = new Intent(this, DeviceList.class);
this.startActivityForResult(serverIntent, REQUEST_DEVICE_CONNECT);
//startActivityForResult(serverIntent, REQUEST_DEVICE_CONNECT);
}
private void bluetoothOn()
{
BTAdapter.enable();
Toast.makeText(this, "Bluetooth is now turned on", Toast.LENGTH_SHORT).show();
}
private void bluetoothOff()
{
BTAdapter.disable();
Toast.makeText(this, "Bluetooth is now turned off.", Toast.LENGTH_SHORT).show();
}
private void abouts()
{
Intent intentAbouts = new Intent(this, abouts.class);
//startActivityForResult(intentAbouts, REQUEST_ABOUTS);
startActivity(intentAbouts);
}
由于我的另一个类 ControllerTab 中也存在错误,因此这里是我的代码: 公共(public)类 ControllerTab 扩展 Activity 实现 OnItemClickListener {
//BluetoothAdapter BTAdapter;
public static BluetoothAdapter BTAdapter;
//Intent
private static final int REQUEST_DEVICE_CONNECT = 1;
private static final int REQUEST_ENABLE_BLUETOOTH = 2;
private static final int REQUEST_ABOUTS = 3;
//
private String connectedDeviceName = null;
//Message types sent from the Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
public static final int RECIEVE_MESSAGE = 6;
//Will be used in BluetoothCommandSrvice jave file
public static final String TOAST = "toast";
public static final String DEVICENAME = "device name";
//@@@@@@@for the controller
public static final String tagStateCTRL = "Controller";
private OutputStream outStream = null;
private static final UUID myUUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static String address = "00:00:00:00:00:00"; // Insert your bluetooth devices MAC address
private StringBuilder sb = new StringBuilder();
Button btn_d1_on, btn_d1_off, btn_d2_on, btn_d2_off;
Switch switch_d1, switch_d2;
TextView textDevice1;
//for the switch
private final static Integer[] ids = { R.id.switch1, R.id.switch2 };
//object for bluetooth command service
private BluetoothCommandService commandService = null;
//listview, swtchcontrol
private ArrayAdapter<String> switchControls;
private TextView deviceID;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
List<Device> deviceList = db.getAllDevices();
if (deviceList.size()!=0) {
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
deviceID = (TextView) view.findViewById(R.id.deviceId);
String valDeviceId = deviceID.getText().toString();
Intent objIndent = new Intent(getApplicationContext(), EditDeviceName.class);
objIndent.putExtra("deviceID", valDeviceId);
startActivity(objIndent);
}
});
// Initialize the BluetoothChatService to perform bluetooth connections
commandService = new BluetoothCommandService(this, bluetoothHandler);
BTAdapter = BluetoothAdapter.getDefaultAdapter();
switch_d1 = (Switch) findViewById(R.id.switch1);
switch_d2 = (Switch) findViewById(R.id.switch2);
registerOnCheckedListener(ids);
}}
private ListView getListView() {
// TODO Auto-generated method stub
return null;
}
@SuppressLint("NewApi")
private void registerOnCheckedListener(Integer... ids)
{
for (int i=0; i<ids.length; i++)
{
Integer id = ids[i];
Switch switchControl = (Switch) findViewById(id);
switchControl.setOnCheckedChangeListener((OnCheckedChangeListener) this);
}
}
//@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
switch (buttonView.getId())
{
case R.id.switch1:
if (isChecked)
//ga error man sooooo "surround with try/catch" pra mwala.. ok? ^_^
try {
String address = new String(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
if (address.equals(MainActivity.HC05))
{
sendData("1");
}
else
{
Toast.makeText(this, "Your phone is not connected to HC-05.", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
try {
String address = new String(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
if (address.equals(MainActivity.HC05))
{
sendData("3");
}
else
{
Toast.makeText(this, "Your phone is not connected to HC-05.", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
case R.id.switch2:
if (isChecked)
try {
String address = new String(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
if (address.equals(MainActivity.HC05))
{
sendData("2");
}
else
{
Toast.makeText(this, "Your phone is not connected to HC-05.", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
try {
String address = new String(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
if (address.equals(MainActivity.HC05))
{
sendData("4");
}
else
{
Toast.makeText(this, "Your phone is not connected to HC-05.", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...
最佳答案
这是您当前拥有的:
list = getListView()
...
private ListView getListView() {
// TODO Auto-generated method stub
return null;
}
您需要实现getListView
。
关于java - 无法确定空指针异常背后的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21922950/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我在64位linux下使用c++,编译器(g++)也是64位的。当我打印某个变量的地址时,例如一个整数,它应该打印一个 64 位整数,但实际上它打印了一个 48 位整数。 int i; cout <<
我是一名优秀的程序员,十分优秀!