7

I'm getting the message : missing package statement. This comes up in red:

Example

It's a simple project which I got here,

selecting contact from autocomplete textview

I just renamed my MainActivity.java to ContactWithAuto.java. My project builds ok but when I try run it on my phone I get:

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.

I tried this solution but it didn't work: Android studio auto fix

Also tried cleaning, building, restarting Android Studio several times. Any ideas?

Here is my code :

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

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="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>
Community
  • 1
  • 1
CHarris
  • 2,693
  • 8
  • 45
  • 71

5 Answers5

15

Add this line on the top of your ContactWithAuto.java

package com.example.chris.autocompletetextview;

Package line is missing.

Rohan Arora
  • 661
  • 5
  • 15
  • 2
    Some answers given were similar but I really have to mark this as correct for being here first and being the exact code I needed to add to my project. Perhaps surprisingly (maybe I'm missing something) but ContactWithAuto or .ContactWithAuto in my manifest makes no difference - my app runs either way. +1 for all your help! – CHarris Mar 22 '16 at 21:03
2

manifest:

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

You need to have a "." in front of class in activity. Copy above code in manifest.

Ravi Gadipudi
  • 1,446
  • 1
  • 17
  • 30
  • Hi Ravi, thanks for that, not at office right now but will try it when back and mark if correct. Although I'm nearly sure it was like that, .ContactWithAuto, was still giving problems so I took out the dot. If you think it could be anything else let me know. – CHarris Mar 22 '16 at 12:31
  • 1
    then add the path with package name like .ContactWithAuto – Ravi Gadipudi Mar 22 '16 at 12:33
2

Just as it says: you missing a package statement.. Add the package name (package <class's package name>) to the class's first line (before the imports)

nbaroz
  • 1,795
  • 13
  • 14
1

Please try this in Manifest file, when you type the complete package name and place the . the class name should automatically come up. This is a kind of validation that manifest has got the right class mapped. Also, please do a "Clean project" once in case of any issues post change.

<?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>
Natarajan Raman
  • 606
  • 1
  • 4
  • 14
1

You need to add : package com.example.chris.autocompletetextview; on your ContactWithAuto.java file,

also you dose not need to import com.example.chris.autocompletetextview.R;

after adding your package on Java class.