Missing package statement main java

Java – Missing package statement: ‘com.VRCoreSoftware.testapp4’

I’m trying to begin learning the ropes of programming Android applications, and so I am following the Android developer tutorials. I’m currently working on the «Starting Another Activity» section, and have followed the sample exactly, with the exception of the app name. When I add the class code for the second activity to be called when a button is clicked, I get the following error: Missing package statement: ‘com.VRCoreSoftware.testapp4’

All of the relevant pieces of code (as far as I am aware of), are shown below:

Main Activity.java:
The only error showing here is that it does not recognize DisplayMessageActivity.class
Intent intent = new Intent(this, DisplayMessageActivity.class);

package com.VRCoreSoftware.testapp4; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; // import android.view.View; import android.content.Intent; import android.widget.EditText; public class MainActivity extends ActionBarActivity < public final static String EXTRA_MESSAGE = "com.VRCoreSoftware.testapp4.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) < getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); >> @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; >@Override public boolean onOptionsItemSelected(MenuItem item) < // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) < case R.id.action_settings: return true; >return super.onOptionsItemSelected(item); > /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment < public PlaceholderFragment() < >@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) < View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; >> public void sendMessage(View view) < // Do something in response to button Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); >> 

DisplayMessageActivity.java
There are many errors, with the source (I’m assuming) being the apparent failed import.

import com.VRCoreSoftware.testapp4.MainActivity; public class DisplayMessageActivity extends MainActivity < @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) < // Show the Up button in the action bar. getActionBar().setDisplayHomeAsUpEnabled(true); >Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); > @Override public boolean onOptionsItemSelected(MenuItem item) < switch (item.getItemId()) < case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; >return super.onOptionsItemSelected(item); > > 

From the AndroidManifest.xml file:
The only error here is that it does not recognize ‘DisplayMessageActivity’ in the line:
android:name=»com.VRCoreSoftware.testapp4.DisplayMessageActivity»

Читайте также:  Тег IMG

I am currently using Android Studio to write and compile this code.
I would greatly appreciate any and all help on this matter.
Thank you very much.

Best Solution

It seems DisplayMessageActivity isn’t in the same package as MainActivity, which also means it’s not in com.VRCoreSoftware.testapp4 , making the manifest wrong. Move it to the correct package and you’re good to go.

Источник

Package statement for ‘com.VRCoreSoftware.testapp4’ is absent

Upon adding the class code for the second activity, an error is triggered stating that the package statement ‘com.VRCoreSoftware.testapp4’ is missing. The code snippets related to this issue are displayed below, with the main issue being that the DisplayMessageActivity class is not recognized in the MainActivity file. In addition, the DisplayMessageActivity file has multiple errors due to a failed import. It was discovered that the package line is only present in the generated parser source file and not in the FooLexer.java file as expected.

Missing package statement: ‘com.VRCoreSoftware.testapp4’

As a beginner in Android app development, I am relying on the Android developer tutorials to learn the basics of programming. At present, I am focused on the «Starting Another Activity» section and have followed the sample code precisely, except for modifying the app name. However, when I incorporate the code for the second activity to be activated by clicking a button, I encounter an error message that reads: missing package statement : ‘com.VRCoreSoftware.testapp4’.

All the applicable lines of code (to the best of my knowledge) are displayed below:

The error displayed in Main Activity.java is that it fails to recognize the displaymessageactivity .class. To resolve this issue, an Intent is created in the same file to call upon the DisplayMessageActivity class.

package com.VRCoreSoftware.testapp4; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; // import android.view.View; import android.content.Intent; import android.widget.EditText; public class MainActivity extends ActionBarActivity < public final static String EXTRA_MESSAGE = "com.VRCoreSoftware.testapp4.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) < getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); >> @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; >@Override public boolean onOptionsItemSelected(MenuItem item) < // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) < case R.id.action_settings: return true; >return super.onOptionsItemSelected(item); > /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment < public PlaceholderFragment() < >@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) < View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; >> public void sendMessage(View view) < // Do something in response to button Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); >> 

In DisplayMessageActivity.java, several errors are present, likely caused by a failed import.

import com.VRCoreSoftware.testapp4.MainActivity; public class DisplayMessageActivity extends MainActivity < @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) < // Show the Up button in the action bar. getActionBar().setDisplayHomeAsUpEnabled(true); >Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); > @Override public boolean onOptionsItemSelected(MenuItem item) < switch (item.getItemId()) < case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; >return super.onOptionsItemSelected(item); > > 

In the AndroidManifest.xml file, the issue lies in the fact that ‘DisplayMessageActivity’ is not being recognized in the line android:name=»com.VRCoreSoftware.testapp4.DisplayMessageActivity».

At the moment, I am utilizing Android Studio for coding and compiling. Any assistance on this topic would be highly appreciated. Many thanks.

The issue is that DisplayMessageActivity is not located in the same package as MainActivity, causing the manifest to be incorrect because it is not in com.VRCoreSoftware.testapp4 . Simply moving it to the appropriate package will resolve the problem.

Lubridate: Statement returns TRUE/FALSE on its own, but incurs, I’ve used tools from the Lubridate package to create POSIXct values for >»All»)$begin_valid) < : missing value where TRUE/FALSE needed.

Источник

Missing package statement main java

I have been coding in java for about a year now and I discovered something strange. Consider the following project structure-

Project | |--src | |--Test | |--Class1.java 

As you can see Class1 has no package statement and Intellij actually gives an error —
Missing package statement: ‘Class1’

I can compile and execute the main method through the command line as follows-

/Project/src/Test $ javac Class1.java /Project/src/Test $ java Class1 Woah no package statement! 

Now I know that when I try to run it through sources root, I will get an exception-

/Project/src $ javac Test/Class1.java /Project/src $ java Test.Class1 Error: Could not find or load main class Test.Class1 Caused by: java.lang.NoClassDefFoundError: Class1 (wrong name: Test/Class1) 

Now I want to know the reason behind having such a design, why doesn’t java straight away give a compile-time error if the package statement is missing and the class is not present in the default package.

Is there any use case for such type of design?

CodePudding user response:

Answering this with the suggestions from the comments to get it out of the unanswered questions queue:

javac can’t know that you want Class1 to be in a package if you compile it with

/Project/src/Test $ javac Class1.java 

But Intellij will sure argue about that, given the folder structure you gave.

Источник

Android : missing package statement; Activity class does not exist

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

Solution 4

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.

Solution 5

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.

How to Solve Android Studio Error Main Activity Class Does Not Exist Error Type 3

How to Android : Error type 3 Error: Activity class < data-lazy-src=

Error package R does not exist R Error

[Tutorial] Error Type 3 Android Studio < data-lazy-src=

CHarris

Comments

Example

I’m getting the message : missing package statement. This comes up in red: 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 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> 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>(); 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)); MapNamePhoneType = new HashMap(); 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) < Mapmap = (Map) av.getItemAtPosition(index); Iterator 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; // >> 

Источник

Оцените статью