简体   繁体   中英

Android Studio unable to find explicit activity class

i have a application that allows me to alter/add/delete from mysql database. The application opens up fine when i run it but when i try clicking on the 'ViewAllEmployee' button i keep getting a error saying it cannot find explicit activity class. I have looked online and tried all solutions that have worked for people with this problem before. I have added it into my AndroidManifest too but it still doesn't want to work. Can anyone help? Literally been trying to fix this for hours!

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:label="@string/app_name" >
<activity
    android:name="com.example.assignment.androidassignment.MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <activity
            android:name="com.example.assignment.androidassignment.ViewAllEmployee"
        />
</activity>
</application>

</manifest>

MainActivity.java -- Error showing when onClick for 'ViewAllEmployee' is clicked:

package com.example.assignment.androidassignment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.HashMap;

public class MainActivity extends Activity implements View.OnClickListener{

//Defining views
private EditText editTextLatitude;
private EditText editTextLongitude;
private EditText editTextTimeInserted;

private Button buttonAdd;
private Button buttonView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing views
    editTextLatitude = (EditText) findViewById(R.id.editTextLat);
    editTextLongitude = (EditText) findViewById(R.id.editTextLon);
    editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted);

    buttonAdd = (Button) findViewById(R.id.buttonAdd);
    buttonView = (Button) findViewById(R.id.buttonView);

    //Setting listeners to button
    buttonAdd.setOnClickListener(this);
    buttonView.setOnClickListener(this);
   }


  //Adding an employee
  private void addEmployee(){

    final String lat = editTextLatitude.getText().toString().trim();
    final String lon = editTextLongitude.getText().toString().trim();
    final String timeInserted = editTextTimeInserted.getText().toString().trim();

    class AddEmployee extends AsyncTask<Void,Void,String>{

        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this,"Adding...","Wait...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... v) {
            HashMap<String,String> params = new HashMap<>();
            params.put(Config.KEY_LAT,lat);
            params.put(Config.KEY_LON,lon);
            params.put(Config.KEY_TIMEINSERTED,timeInserted);

            RequestHandler rh = new RequestHandler();
            String res = rh.sendPostRequest(Config.URL_ADD, params);
            return res;
        }
    }

    AddEmployee ae = new AddEmployee();
    ae.execute();
     }

   @Override
   public void onClick(View v) {
    if(v == buttonAdd){
        addEmployee();
    }

    if(v == buttonView){
        startActivity(new Intent(this,com.example.assignment.androidassignment.ViewAllEmployee.class));
    }
  }
 }

LogCat errors:

12-09 03:34:46.634 2489-2489/? E/AndroidRuntime: FATAL EXCEPTION: main
 =Process: com.example.assignment.androidassignment, PID: 2489
 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.assignment.androidassignment/com.example.assignment.androidassignment.ViewAllEmployee}; have you declared this activity in your AndroidManifest.xml?
 at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
 at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
 at android.app.Activity.startActivityForResult(Activity.java:3424)
 at android.app.Activity.startActivityForResult(Activity.java:3385)
 at android.app.Activity.startActivity(Activity.java:3627)
 at android.app.Activity.startActivity(Activity.java:3595)
 at com.example.assignment.androidassignment.MainActivity.onClick(MainActivity.java:91)
 at android.view.View.performClick(View.java:4438)
 at android.view.View$PerformClick.run(View.java:18422)
 at android.os.Handler.handleCallback(Handler.java:733)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5001)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 at dalvik.system.NativeStart.main(Native Method)

You are nesting <activity> tags in your Manifest. Take them apart:

<activity
    android:name="com.example.assignment.androidassignment.MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />    
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
     android:name="com.example.assignment.androidassignment.ViewAllEmployee"/>

from

if(v == buttonView){
    startActivity(new Intent(this,com.example.assignment.androidassignment.ViewAllEmployee.class));
}

to

if(v == buttonView){
    startActivity(new Intent(MainActivity.this,ViewAllEmployee.class));
}

from

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:label="@string/app_name" >
<activity
    android:name="com.example.assignment.androidassignment.MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <activity
            android:name="com.example.assignment.androidassignment.ViewAllEmployee"
        />
</activity>
</application>

</manifest>

to

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:label="@string/app_name" >
<activity
    android:name="com.example.assignment.androidassignment.MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>


</activity>
 <activity
            android:name="com.example.assignment.androidassignment.ViewAllEmployee"
        />
</application>

</manifest>

please change the above and this is happening because of nesting of activity inside manifest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM