简体   繁体   English

ActivityNotFoundException-活动已在清单文件中声明

[英]ActivityNotFoundException - activity has been declared in the manifest file

This is the place where the error is showing ( LoginActivity.java file) 这是显示错误的地方( LoginActivity.java文件)

Intent intent = new Intent(LoginActivity.this, Login.class);    
startActivity(intent);    //showing error here
finish();    

The login class I'm trying to call is under slideActivity and extends android.support.v4.app.Fragment and LoginActivity is a separate activity under separate package and extends Activity . 我要调用的登录类位于slideActivity下,并扩展了android.support.v4.app.FragmentLoginActivity是位于单独包下的单独活动,并扩展了Activity

I have added slideActivity and LoginActivity to android manifest file. 我已将slideActivityLoginActivity添加到android清单文件。 But the error is because of the Login class. 但是错误是由于Login类。

The error is shown below: 错误如下所示:

12969-12969/com.client.businesstracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.client.businesstracker/com.client.businesstracker.Login}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1624)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
        at android.app.Activity.startActivityForResult(Activity.java:3388)
        at android.app.Activity.startActivityForResult(Activity.java:3349)
        at android.app.Activity.startActivity(Activity.java:3584)
        at android.app.Activity.startActivity(Activity.java:3552)
        at com.client.businesstracker.activity.LoginActivity$3.onResponse(LoginActivity.java:150)
        at com.client.businesstracker.activity.LoginActivity$3.onResponse(LoginActivity.java:120)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)

here is my android manifest file 这是我的Android清单文件

<activity     
        android:name=".SlideActivity"     
        android:label="@string/app_name" >     
        <intent-filter>     
            <action android:name="android.intent.action.MAIN" />

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

        </intent-filter>     
    </activity>     
    <activity android:name=".activity.LoginActivity"     
        android:label="@string/app_name" />     


<activity android:name=".activity.RegisterActivity" 
 android:label="@string/app_name" />    

Here is the Login.java file 这是Login.java文件

package com.client.businesstracker;
import com.client.businesstracker.activity.LoginActivity;
import com.client.businesstracker.loginandregistration.helper.SQLiteHandler;
import com.client.businesstracker.loginandregistration.helper.SessionManager;


public class Login extends android.support.v4.app.Fragment {

private TextView txtName;
private TextView txtServerID;
private TextView txtClientID;
private Button btnLogout;

private SQLiteHandler db;
private SessionManager session;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootview1 = inflater.inflate(R.layout.activity_main, container, false);

    txtName = (TextView) rootview1.findViewById(R.id.name);
    txtServerID = (TextView) rootview1.findViewById(R.id.server_ID);
    txtClientID = (TextView) rootview1.findViewById(R.id.client_ID);
    btnLogout = (Button) rootview1.findViewById(R.id.btnLogout);

    // SqLite database handler
    db = new SQLiteHandler(getActivity());

    // session manager
    session = new SessionManager(getActivity());

    if (!session.isLoggedIn()) {
        logoutUser();
    }

    // Fetching user details from sqlite
    HashMap<String, String> user = db.getUserDetails();

    String name = user.get("name");
    String serverID = user.get("serverID");
    String clientID = user.get("clientID");

    // Displaying the user details on the screen
    txtName.setText(name);
    txtServerID.setText(serverID);
    txtClientID.setText(clientID);


    // Logout button click event
    btnLogout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            logoutUser();
        }
    });
    return rootview1;
}

/**
 * Logging out the user. Will set isLoggedIn flag to false in shared
 * preferences Clears the user data from sqlite users table
 * */
private void logoutUser() {
    session.setLogin(false);

    db.deleteUsers();

    // Launching the login activity
    Intent intent = new Intent(this.getActivity(), LoginActivity.class);
    startActivity(intent);
    getActivity().finish();
}

} }

And here is the SlideActivity.java file from where I'm creating the object of the Login file as navigationDrawerItem 这是SlideActivity.java文件,从这里我将创建登录文件的对象作为navigationDrawerItem

package com.client.businesstracker;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SlideActivity extends AppCompatActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;
//private ListView mDrawerList;
//private ArrayAdapter<String> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slide);
   // mDrawerList = (ListView)findViewById(R.id.left_drawer);
   // addDrawerItems();
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment objFragment = null;
    Log.i("MyActivity", "onNavigationDrawerItemSelected() — get item number " + position);
    switch (position)
    {
        case 0:
            objFragment = new Login();
            break;
        case 1:
            objFragment = new Connect();
            break;
        case 2:
            objFragment = new Home();
            break;
        case 3:
            objFragment = new Preferences();
            break;
        case 4:
            objFragment = new AboutUs();
            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 0:
            mTitle = getString(R.string.title_section1);
            break;
        case 1:
            mTitle = getString(R.string.title_section2);
            break;
        case 2:
            mTitle = getString(R.string.title_section3);
            break;
        case 3:
            mTitle = getString(R.string.title_section4);
            break;
        case 4:
            mTitle = getString(R.string.title_section5);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.slide, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_slide, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((SlideActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

} }

And here is the LoginActivity.java 这是LoginActivity.java

package com.client.businesstracker.activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import com.client.businesstracker.Login;
import com.client.businesstracker.R;
import com.client.businesstracker.loginandregistration.app.AppConfig;
import com.client.businesstracker.loginandregistration.app.AppController;
import com.client.businesstracker.loginandregistration.helper.SQLiteHandler;
import com.client.businesstracker.loginandregistration.helper.SessionManager;

public class LoginActivity extends Activity {
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputServerID;
private EditText inputClientID;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    inputServerID = (EditText) findViewById(R.id.serverID);
    inputClientID = (EditText) findViewById(R.id.clientID);
    inputPassword = (EditText) findViewById(R.id.password);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    // SQLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // Session manager
    session = new SessionManager(getApplicationContext());

    // Check if user is already logged in or not
    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(LoginActivity.this, Login.class);
        startActivity(intent);
        finish();
    }

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String serverID = inputServerID.getText().toString().trim();
            String clientID = inputClientID.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            // Check for empty data in the form
            if (!serverID.isEmpty() && !password.isEmpty() && !clientID.isEmpty()) {
                // login user
                checkLogin(serverID, clientID,password);
            } else {
                // Prompt user to enter credentials
                Toast.makeText(getApplicationContext(),
                        "Please enter the credentials!", Toast.LENGTH_LONG)
                        .show();
            }
        }

    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });

}

/**
 * function to verify login details in mysql db
 * */
private void checkLogin(final String serverID, final String clientID, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_login";

    pDialog.setMessage("Logging in ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    // user successfully logged in
                    // Create login session
                    session.setLogin(true);

                    // Now store the user in SQLite
                    String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String serverID = user.getString("serverID");
                    String clientID = user.getString("clientID");

                    // Inserting row in users table
                    db.addUser(name, serverID, clientID);

                    // Launch main activity
                    Intent intent = new Intent(LoginActivity.this, Login.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("serverID", serverID);
            params.put("clientID", clientID);
            params.put("password", password);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

} }

Basically you didn't defined the Login.class in Manifest file. 基本上,您没有在清单文件中定义Login.class

You have to add Login Activity in Manifest like below: 您必须在清单中添加“登录活动”,如下所示:

 <activity android:name=".activity.Login"     
        android:label="@string/app_name" />   

Hope this will help you. 希望这会帮助你。

首先检查异常,然后再考虑com.client.businesstracker.Login是否不在清单中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActivityNotFoundException:但已在清单中声明了活动 - ActivityNotFoundException: but activity has been declared in manifest 未在清单中声明的​​活动(显然) - Activity not declared in Manifest (Apparently) ActivityNotFoundException:没有找到处理 Intent {(有额外功能)} 的活动 - ActivityNotFoundException: No Activity found to handle Intent { (has extras) } ActivityNotFoundException:无法找到显式活动类GoogleDriveProxeyActivity是否已声明此内容 - ActivityNotFoundException: Unable to find explicit activity class GoogleDriveProxeyActivity have you declared this 无法实例化活动componentInfo,在清单中声明了Activity - Unable to instantiate activity componentInfo, Activity declared in Manifest 有关启动清单中的类的ActivityNotFoundException - ActivityNotFoundException on launching class that is in manifest 即使在清单中声明了活动,也找不到活动异常 - Activity not found exception even if it is declared in manifest 运行应用程序时出错“未在清单中声明 SplashScreen 活动” - Error Running App "The SplashScreen Activity is not Declared in Manifest" 活动错误甚至以为已在清单中声明 - Activity error even thought its declared in manifest 何时声明destroy()? - When the destroy() has been declared?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM