尝试调用 Android 中另一个类中的方法。我今天才刚刚开始 Android 开发,所以这对我来说是全新的。我想做的就是通过单击按钮来运行我的代码。
但是,在我的按钮代码中,我遇到了一个问题,IDE 告诉我无法将其解析为类型。谁能帮我解决这个问题?
然后是我的主要 Activity 课。这是错误所在:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button aButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListeners();
}
@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;
}
private void addListeners() {
Button btnCallMethod = (Button) this.findViewById(R.id.btnCallMethod);
// CALL METHOD BUTTON
btnCallMethod.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// error is on this line below
Intent intent = new Intent(getApplicationContext(), DeviceScanActivity.class);
startActivity(intent); // start the DeciveScanActivity
}
return false;
}
});
}
}
更新我的 DeviceScanActivity 类的代码:
import android.annotation.TargetApi;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
@TargetApi(18)
public class DeviceScanActivity extends ListActivity {
private Handler mHandler;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private ArrayAdapter<Object> list;
protected void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
int key = b.getInt("yourKey");
switch(key)
{
case 1: // call method here
list = new ArrayAdapter<Object>(getApplicationContext(),
android.R.layout.simple_list_item_1);
setListAdapter(list);
scanLeDevice(true);
}
}
@TargetApi(18)
public void scanLeDevice(final boolean enable) {
list.add("Scanning...");
final BluetoothAdapter adapter = getBluetoothAdapter();
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// mBluetoothAdapter.stopLeScan(mLeScanCallback);
adapter.stopLeScan(callback);
list.clear();
}
}, SCAN_PERIOD);
adapter.startLeScan(callback);
} else {
adapter.stopLeScan(callback);
}
}
@TargetApi(18)
private BluetoothAdapter getBluetoothAdapter()
{
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
return manager.getAdapter();
}
private final BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback()
{
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// TODO Auto-generated method stub
{
list.add("found: " + device);
runOnUiThread(new Runnable(){
@Override
public void run()
{
list.add(device);
list.notifyDataSetChanged();
}
});
}
}
};
}
还有我的 list Xml,我认为它对于 Android 很重要:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bletest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
最佳答案
您需要确保在添加新类(在本例中为DeviceScanActivity
)时,将其添加到正确的包中。
当您右键单击项目并单击“新建 -> 类”时,它不会为您填充包名称。有两种方法可以确保将其添加到正确的包中:
不要右键单击项目来添加类,而是右键单击包。
- Your projects packages can be found in either the Package Explorer or Project explorer by expanding the
src
folder. the next layer down should be packages.
- Your projects packages can be found in either the Package Explorer or Project explorer by expanding the
您可以手动将包名称添加到
新建 Java 类
对话框中。 “包”应该位于顶部附近。
注意,这假设您使用的是 Eclipse IDE/ADT。如果没有,具体步骤可能会有所不同。
其次,您需要将任何新的 Activity
类添加到 list 中。如果不是,您将不会收到 Can't be returned to a type
错误,但您会收到运行时错误,因此请继续修复它。至少,您需要类似的东西:
<activity
android:name="com.example.bletest.DeviceScanActivity"
</activity>
关于java - Android 和 Java 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637977/