【问题标题】:Android : missing package statement; Activity class does not existAndroid:缺少包声明;活动类不存在
【发布时间】:2016-07-09 08:05:42
【问题描述】:

我收到消息:缺少包装声明。这是红色的:

这是我来这里的一个简单项目,

selecting contact from autocomplete textview

我刚刚将 MainActivity.java 重命名为 ContactWithAuto.java。我的项目构建正常,但是当我尝试在手机上运行它时,我得到:

Launching application: com.example.chris.autocompletetextview/ContactWithAuto.
DEVICE SHELL COMMAND: am start -n "com.example.chris.autocompletetextview/ContactWithAuto" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
open: Permission denied
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.chris.autocompletetextview/ContactWithAuto }
Error type 3
Error: Activity class {com.example.chris.autocompletetextview/ContactWithAuto} does not exist.

我尝试了这个解决方案,但没有奏效: Android studio auto fix

还尝试多次清理、构建、重新启动 Android Studio。有什么想法吗?

这是我的代码:

ContactWithAuto.java:

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;

import com.example.chris.autocompletetextview.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ContactWithAuto extends Activity {

    private ArrayList<Map<String, String>> mPeopleList;
    private SimpleAdapter mAdapter;
    private AutoCompleteTextView mTxtPhoneNo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_with_auto);
        mPeopleList = new ArrayList<Map<String, String>>();
        PopulatePeopleList();
        mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
        mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
                new String[] { "Name", "Phone", "Type" }, new int[] {
                R.id.ccontName, R.id.ccontNo, R.id.ccontType });
        mTxtPhoneNo.setAdapter(mAdapter);

    }

    public void PopulatePeopleList() {
        mPeopleList.clear();
        Cursor people = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (people.moveToNext()) {
            String contactName = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if ((Integer.parseInt(hasPhone) > 0)){
                // You know have the number so now query it like this
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                        null, null);
                while (phones.moveToNext()){
                    //store numbers and display a dialog letting the user select which.
                    String phoneNumber = phones.getString(
                            phones.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String numberType = phones.getString(phones.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.TYPE));
                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    if(numberType.equals("0"))
                        NamePhoneType.put("Type", "Work");
                    else
                    if(numberType.equals("1"))
                        NamePhoneType.put("Type", "Home");
                    else if(numberType.equals("2"))
                        NamePhoneType.put("Type",  "Mobile");
                    else
                        NamePhoneType.put("Type", "Other");
                    //Then add this map to the list.
                    mPeopleList.add(NamePhoneType);
                }
                phones.close();
            }
        }
        people.close();
        startManagingCursor(people);
    }

    public void onItemClick(AdapterView<?> av, View v, int index, long arg){
        Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
        Iterator<String> myVeryOwnIterator = map.keySet().iterator();
        while(myVeryOwnIterator.hasNext()) {
            String key=(String)myVeryOwnIterator.next();
            String value=(String)map.get(key);
            mTxtPhoneNo.setText(value);
        }
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_contact_with_auto, menu);
//        return true;
//    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chris.autocompletetextview" >

    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ContactWithAuto"
            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>

</manifest>

【问题讨论】:

  • 请在 manifest.xml 中更改这一行 android:name=".ContactWithAuto"

标签: android android-activity


【解决方案1】:

将此行添加到您的 ContactWithAuto.java 的顶部

package com.example.chris.autocompletetextview;

包装线丢失。

【讨论】:

  • 给出的一些答案是相似的,但我真的必须将其标记为正确,以便首先来到这里并且是我需要添加到我的项目中的确切代码。也许令人惊讶(也许我遗漏了一些东西),但我的清单中的 ContactWithAuto 或 .ContactWithAuto 没有任何区别——我的应用程序以任何一种方式运行。 +1 为您提供所有帮助!
【解决方案2】:

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chris.autocompletetextview" >

<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ContactWithAuto"
        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>

你需要有一个“。”在课前活动。将上述代码复制到清单中。

【讨论】:

  • 嗨 Ravi,谢谢你,现在不在办公室,但会在回来时尝试并标记是否正确。尽管我几乎可以肯定它是那样的,但 .ContactWithAuto 仍然存在问题,所以我取出了那个点。如果您认为这可能是其他任何事情,请告诉我。
  • 然后添加带有包名的路径,例如 .ContactWithAuto
【解决方案3】:

正如它所说:您缺少包装声明.. 将包名 (package &lt;class's package name&gt;) 添加到类的第一行(在导入之前)

【讨论】:

    【解决方案4】:

    请在 Manifest 文件中尝试此操作,当您键入完整的包名称并将 .类名应该会自动出现。这是一种验证清单已映射正确的类。此外,请执行一次“清理项目”,以防更改后出现任何问题。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.chris.autocompletetextview" >
    
        <uses-permission android:name="android.permission.READ_CONTACTS" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.chris.autocompletetextview.ContactWithAuto"
                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>
    
    </manifest>
    

    【讨论】:

      【解决方案5】:

      您需要添加: 包 com.example.chris.autocompletetextview;在您的 ContactWithAuto.java 文件中,

      你也不需要导入 com.example.chris.autocompletetextview.R;

      在 Java 类上添加你的包之后。

      【讨论】:

        猜你喜欢
        • 2013-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 2013-07-11
        相关资源
        最近更新 更多