2013-09-05 51 views
2

尝试调用Android中另一个类中的方法。我今天刚刚开始Android开发,所以这对我来说都是新的。我想要做的只是点击一个按钮来运行我的代码。Android和Java无法解析为类型

但是,在我的按钮代码中,我遇到了IDE告诉它无法解析为类型的问题。任何人都可以帮我解决这个问题吗?

然后我的主要活动类。这是错误的是:

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

} 

而且我的清单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> 

+0

您正在使用哪个IDE?您是否安装了Android SDK并将IDE配置为支持Android? – hexafraction

+0

Ecplise。我下载了Android SDK并从那里运行它。从我读的,它应该是自动完成的。我可以让Hello World应用在我的设备上正常运行。 – N0xus

+0

请粘贴logcat –

回答

3

你需要确保当你添加一个新类(DeviceScanActivity在这种情况下),你将它添加到正确的程序包。

当您右键单击您的项目并单击“新建 - >类”时,它不会为您填充软件包名称。有两种方法,以确保它被添加到合适的包:

  • 而不是右键单击项目增加一类,用鼠标右键单击

    • 你的项目包既可以在包资源管理器或项目资源管理器中通过扩大src文件夹中找到。下一层应该是包。
  • 您可以手动添加软件包的名称为New Java Class对话框。 “包装”应该靠近顶部。

请注意,这里假定您使用的是Eclipse IDE/ADT。如果没有,确切的步骤可能会有所不同。其次,您需要将任何新的Activity类添加到您的清单。如果不是,你将不会得到一个Can't be resolved to a type错误,但是你会得到一个运行时错误,所以在你出现的时候继续修复它。在最低限度,你需要像这样:

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

试试这个:

btnCallMethod.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

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

我仍然在DeviceScanActivity.class中得到与我原来的帖子相同的问题。 – N0xus

0

我得到了同样的错误。在完成所有可能的事情之后,我就清理了这个项目。然后错误消失了。但是请确保在清理之前备份项目,因为有时R文件可能会消失。