简体   繁体   English

Android和Java无法解析为某种类型

[英]Android & Java cannot be resolved to a type

Trying to call a method held in another class in Android. 尝试调用Android中另一个类中包含的方法。 I've only just started Android development today so this is all new to me. 我今天才刚刚开始Android开发,所以这对我来说是全新的。 All I'm wanting to do is have my code run with the click of a button. 我要做的只是单击一下按钮即可运行我的代码。

However, in my button code, I've got an issue where the IDE tells it cannot be resolved to a type. 但是,在我的按钮代码中,我遇到了一个问题,IDE告知它无法解析为一种类型。 Can anyone help me in fixing this? 谁能帮我解决这个问题?

Then my main activity class. 然后是我的主要活动课。 This is where the error is: 这是错误所在:

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;
           }
         });
        }
}

Update the code for my DeviceScanActivity class: 更新我的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();
                    }
                });
        }
    }
    };

}

Also my manifest Xml which I think is important for Android: 还有我认为对Android很重要的清单Xml:

<?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>

You need to make sure that when you add a new class( DeviceScanActivity in this case), you add it to the correct package. 您需要确保在添加新类(在这种情况下为DeviceScanActivity )时,将其添加到正确的包中。

When you right-click on your project and click "New -> Class", it doesn't populate the package name for you. 右键单击项目并单击“新建->类”时,它不会为您填充程序包名称。 There are two ways to make sure it gets added to the proper package: 有两种方法可以确保将其添加到正确的程序包中:

  • Instead of right-clicking the project to add a class, right-click the package . 不用右键单击项目来添加类,而是右键单击

    • Your projects packages can be found in either the Package Explorer or Project explorer by expanding the src folder. 通过展开src文件夹,可以在“程序包资源管理器”或“项目资源管理器”中找到您的项目程序包。 the next layer down should be packages. 下一层应该是包装。
  • You can add the package name manually to the New Java Class dialog. 您可以将包名称手动添加到“ New Java Class对话框。 "Package" should be near the top. “包装”应靠近顶部。

Note, this assumes you are using Eclipse IDE / ADT. 注意,这假设您正在使用Eclipse IDE / ADT。 If not, the exact steps might be different. 如果没有,确切的步骤可能会有所不同。


Secondly, you need to add any new Activity classes to your manifest. 其次,您需要将任何新的Activity类添加到清单中。 You won't get a Can't be resolved to a type error if it's not, but you'll get a runtime error, so go ahead and fix it while you're there. 如果不是,您将不会得到Can't be resolved to a type错误,但是会遇到运行时错误,因此请继续解决并在可能的情况下进行修复。 At a very minimum, you need something like: 至少,您需要以下内容:

<activity
    android:name="com.example.bletest.DeviceScanActivity"
</activity>

Try this: 尝试这个:

btnCallMethod.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(), DeviceScanActivity.class);
               startActivity(intent);
            }
        });

I got the same error.I just cleaned the project after doing all the possible things.Then errors went. 我遇到了同样的错误,做了所有可能的事情后才清理项目,然后出错了。 But make sure to have a backup of the project before you clean it because sometimes the R file may disappear. 但是,在清理项目之前,请确保对其进行备份,因为有时R文件可能会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM