【发布时间】:2014-10-25 07:03:08
【问题描述】:
这是我最近发布的一个后续场景,我尝试从另一个类调用一个方法并以更改 TextView 来响应。现在我正在尝试添加以在该方法(BluetoothOn)上初始化蓝牙适配器,但是,我再次面临与应用程序停止相同的错误。另一方面,如果我在 Main 方法上声明 BLEadapter,我可以毫无问题地运行应用程序。
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private TextView textView;
//private View myView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView =(TextView)findViewById(R.id.textView1);
addListenerOnButton();
}
private void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
enableBluetooth(arg0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void enableBluetooth (View view){
BluetoothOn ble = new BluetoothOn();
ble.initializeBlue(textView);
}
}
在我的蓝牙课上
public class BluetoothOn extends MainActivity {
private TextView textView1;
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
public void initializeBlue(View myView){
String BleisOn = "Bluetooth enabled !!!!";
textView1 = (TextView)myView.findViewById(R.id.textView1);
textView1.setText(BleisOn);
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
谁能告诉我如何从我的主要活动中调用不会导致应用程序崩溃的方法。我只是一个初学者,请详细解释一下,我需要什么东西,我只想在 main 之外创建其他类,并在用户在 Android 应用程序上进行交互时调用这些方法。我认为我只是没有正确地将值传递给其他类,这就是为什么它在 android 上不起作用......一旦我点击分配的按钮,应用程序就会崩溃。这段代码我需要什么。顺便说一下,这是我之前帖子中的链接,creating a separate class without a user interface 请帮助谢谢各位好人...
这是日志猫
09-01 13:26:27.307: I/ActivityManager(19336): Timeline: Activity_idle id: android.os.BinderProxy@42d54610 time:341763403
09-01 13:26:39.627: D/AndroidRuntime(21289): Shutting down VM
09-01 13:26:39.627: W/dalvikvm(21289): threadid=1: thread exiting with uncaught exception (group=0x4156cd88)
09-01 13:26:39.627: E/AndroidRuntime(21289): FATAL EXCEPTION: main
09-01 13:26:39.627: E/AndroidRuntime(21289): Process: com.example.thisapp, PID: 21289
09-01 13:26:39.627: E/AndroidRuntime(21289): java.lang.IllegalStateException: System services not available to Activities before onCreate()
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.app.Activity.getSystemService(Activity.java:4532)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.BluetoothOn.initializeBlue(BluetoothOn.java:27)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.MainActivity.enableBluetooth(MainActivity.java:71)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.MainActivity$1.onClick(MainActivity.java:43)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.view.View.performClick(View.java:4569)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.view.View$PerformClick.run(View.java:18553)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Handler.handleCallback(Handler.java:733)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Handler.dispatchMessage(Handler.java:95)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Looper.loop(Looper.java:212)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.app.ActivityThread.main(ActivityThread.java:5151)
09-01 13:26:39.627: E/AndroidRuntime(21289): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 13:26:39.627: E/AndroidRuntime(21289): at java.lang.reflect.Method.invoke(Method.java:515)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
09-01 13:26:39.627: E/AndroidRuntime(21289): at dalvik.system.NativeStart.main(Native Method)
09-01 13:26:41.487: I/Process(21289): Sending signal. PID: 21289 SIG: 9
【问题讨论】:
-
您说“应用已停止”。发布 logcat 输出。
-
猜猜:你将
TextView传递给initializeBlue()。该方法试图在View中找到TextView- 这就是为什么textView1可能是null。 -
“我怎样才能从我的主要活动中调用不会导致应用程序崩溃的方法”这是要求我们解释面向对象的编程和设计。有很多关于这个主题的书籍,太大了,无法在这里写一个简短的答案。
-
您对上一个问题的回答错误。看看 luisdurazoa 的回答。
标签: android class methods call