5

这是我在 Android 中的第一个项目,这意味着我是编程新手。我知道论坛上有很多页面描述了我的问题的解决方案,但现在我失去了 2 天仍然没有得到解决方案。请帮我!

首先是我的错误:

01-02 11:08:27.314: D/AndroidRuntime(1045): Shutting down VM
01-02 11:08:27.314: W/dalvikvm(1045): threadid=1: thread exiting with uncaught exception (group=0x409e61f8)
01-02 11:08:27.324: E/AndroidRuntime(1045): FATAL EXCEPTION: main
01-02 11:08:27.324: E/AndroidRuntime(1045): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.wifiscan/com.example.wifiscan.activity_wifi_scan}: java.lang.ClassNotFoundException: com.example.wifiscan.activity_wifi_scan
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.os.Looper.loop(Looper.java:137)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at dalvik.system.NativeStart.main(Native Method)
01-02 11:08:27.324: E/AndroidRuntime(1045): Caused by: java.lang.ClassNotFoundException: com.example.wifiscan.activity_wifi_scan
01-02 11:08:27.324: E/AndroidRuntime(1045):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
01-02 11:08:27.324: E/AndroidRuntime(1045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
01-02 11:08:27.324: E/AndroidRuntime(1045):     ... 11 more

我的 WifiScan:

 public class WifiScan extends Activity implements OnClickListener {      
     WifiManager wifi;       
     ListView lv;
     TextView textStatus;
     Button buttonScan;
     int size = 0;
     List<ScanResult> results;
 
     String ITEM_KEY = "key";
     ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
     SimpleAdapter adapter;
 
     /* Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_wifi_scan);
 
         buttonScan = (Button) findViewById(R.id.scan);
         buttonScan.setOnClickListener(this);
         lv = (ListView)findViewById(R.id.list);
 
         wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
         if (wifi.isWifiEnabled() == false)
         {
             Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
             wifi.setWifiEnabled(true);
         }   
         wifi.startScan(); //js dopisal
      //   this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
         lv.setAdapter(this.adapter);
 
              registerReceiver(new BroadcastReceiver()
         {
             @Override
             public void onReceive(Context c, Intent intent) 
             {
                results = wifi.getScanResults();
                size = results.size();
             }
         }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));                    
     }
 
     public void onClick(View view) 
     {
         arraylist.clear();          
         wifi.startScan();
 
              Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
         try 
         {
             size = size - 1;
             while (size >= 0) 
             {   
                 HashMap<String, String> item = new HashMap<String, String>();                       
                 item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);
 
                 arraylist.add(item);     
                size--;
                 adapter.notifyDataSetChanged();                 
             } 
         }
         catch (Exception e)
         { }         
     }    
 }

我的 activity_wifi_scan.xml 文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/TableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".WifiScan" >

    <Button    
        android:id="@+id/scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="scan"
        android:text="SCAN" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</TableLayout>

还有我的 AndroidManifest 文件:

<?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.wifiscan"
     android:versionCode="1"
          android:versionName="1.0" >
    
        <uses-premission android:name="android.permission.ACCESS_WIFI_STATE"/>
         <uses-permission android:name="android.permission.WAKE_LOCK"/>
        <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
     <uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
     <uses-permission android:name="android.permission.INTERNET"> </uses-permission>

     <uses-sdk
          android:minSdkVersion="8"
         android:targetSdkVersion="17" />
     
     <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
     
     <activity android:name=".activity_wifi_scan">
         <intent-filter>
                 <action android:name="android.intent.action.MAIN"></action>
                 <category android:name="android.intent.category.LAUNCHER"></category>
                 <data />
         </intent-filter>
     </activity>
     
     <receiver android:name=".activity_wifi_scan">
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
         </intent-filter>
     </receiver>        
     </application>
 </manifest>
4

5 回答 5

3

您的活动名称是WifiScan,但您已将其声明为.activity_wifi_scan更改并重试。

<activity>将元素更改为:

<activity android:name=".WifiScan">
         <intent-filter>
                 <action android:name="android.intent.action.MAIN"></action>
                 <category android:name="android.intent.category.LAUNCHER"></category>
                 <data />
         </intent-filter>
     </activity>
于 2013-05-16T10:04:49.000 回答
0

我遇到了这个错误,经过数小时的搜索,我发现这是我的错误(哦!但这是不可能的!哈哈)。

对我来说,问题是我已将其添加android:theme="@style/Theme.appcompat.Light"到布局 XML 而不是应用程序清单中。

一旦我从布局中删除了属性并替换了清单上的属性,一切都按预期工作。

于 2013-09-24T02:01:56.463 回答
0

尝试将您的活动添加com.example.wifiscan.activity_wifi_scan到您的AndroidManifest.xml

于 2013-05-16T09:47:30.367 回答
0

您没有在清单文件中初始化您的活动

于 2013-05-16T09:51:46.470 回答
0

解决方案1:尝试清理您的项目,重新启动eclipse。解决方案 2:检查清单文件。解决方案 3:检查清单文件中的标签和标签是否与活动的正确位置匹配。

如果您可以发布您的代码,这将有所帮助。

于 2013-05-16T09:53:46.797 回答