无法启动新活动,表示它未在Manifest上声明

时间:2014-10-31 03:40:50

标签: android android-intent android-activity android-manifest

我看过很多与此类似的帖子,但我无法弄清楚到底是什么问题。

这是logcat的一部分:

10-30 20:37:52.510  11225-11225/com.timetodev.prototipoulsa E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.timetodev.prototipoulsa, PID: 11225
android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.timetodev.prototipoulsa.Home}; have you declared this activity in your AndroidManifest.xml?

我的AndroidManifest.xml:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Login"
        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=".Home"
        android:label="@string/title_activity_home" >
    </activity>
</application>

我也试过android:name =“com.timetodev.prototipoulsa.Home”

这是我执行Intent的代码的一部分。

public class Login extends Activity {
Button btnLogin;
private EditText usernameField,passwordField;
private TextView loginErrorMsg;
Intent intent = new Intent(Login.this, Home.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    usernameField = (EditText)findViewById(R.id.username);
    passwordField = (EditText)findViewById(R.id.password);
    btnLogin = (Button) findViewById(R.id.btn_login);
    loginErrorMsg = (TextView) findViewById(R.id.loginErrorMsg);
     /*Evento para Login*/
    btnLogin.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            if( (!usernameField.getText().toString().equals("")) && ( !passwordField.getText().toString().equals("")) ){
                String username = usernameField.getText().toString();
                String password = passwordField.getText().toString();
                if(doLogin(username, password)){
                    loginErrorMsg.setText("");
                    Toast.makeText(getApplicationContext(),"Conectado",Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                }else{
                    loginErrorMsg.setText("Datos incorrectos");
                }
            }
            else if( ( !usernameField.getText().toString().equals("")) ){
                Toast.makeText(getApplicationContext(), "Campo de contraseña vacío", Toast.LENGTH_SHORT).show();
            }
            else if( ( !passwordField.getText().toString().equals("")) ){
                Toast.makeText(getApplicationContext(),"Campo de matrícula vacío", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Campos de matrícula y contraseña vacíos", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

public boolean doLogin(String username,String password){
    if (username.equals("admin") && password.equals("admin"))
        return true;
    else
        return false;
}

这是XML代码,以防它有用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Login">



<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/username"
    android:hint="Matrícula"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:inputType="text" />

<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/password"
    android:hint="Contraseña"
    android:layout_below="@+id/username"
    android:layout_alignStart="@+id/username"
    android:layout_marginTop="10dp"
    android:inputType="textPassword" />

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="Iniciar sesión"
    android:id="@+id/btn_login"
    android:layout_below="@+id/password"
    android:layout_centerHorizontal="true"
    android:background="#ff66a3ff"
    android:textColor="#ffffffff"
    android:layout_marginTop="10dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/logo"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/btn_login"
    android:layout_marginTop="30dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Creado por Fernando Salas"
    android:id="@+id/credit"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/loginErrorMsg"
    android:layout_below="@+id/btn_login"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:textColor="#ffff453d" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

当我用不同的代码在课堂上声明意图时,我得到了NullpointerException。删除类中的Intent intent = new Intent(Login.this, Home.class);并在startActivity(intent)之前在onclick中声明它;试一试吧。

public class Login extends Activity {
Button btnLogin;
private EditText usernameField,passwordField;
private TextView loginErrorMsg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    usernameField = (EditText)findViewById(R.id.username);
    passwordField = (EditText)findViewById(R.id.password);
    btnLogin = (Button) findViewById(R.id.btn_login);
    loginErrorMsg = (TextView) findViewById(R.id.loginErrorMsg);
     /*Evento para Login*/
    btnLogin.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            if( (!usernameField.getText().toString().equals("")) && ( !passwordField.getText().toString().equals("")) ){
                String username = usernameField.getText().toString();
                String password = passwordField.getText().toString();
                if(doLogin(username, password)){
                    loginErrorMsg.setText("");
                    Toast.makeText(getApplicationContext(),"Conectado",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Login.this, Home.class);
                    startActivity(intent);
                }else{
                    loginErrorMsg.setText("Datos incorrectos");
                }
            }
            else if( ( !usernameField.getText().toString().equals("")) ){
                Toast.makeText(getApplicationContext(), "Campo de contraseña vacío", Toast.LENGTH_SHORT).show();
            }
            else if( ( !passwordField.getText().toString().equals("")) ){
                Toast.makeText(getApplicationContext(),"Campo de matrícula vacío", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Campos de matrícula y contraseña vacíos", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

public boolean doLogin(String username,String password){
    if (username.equals("admin") && password.equals("admin"))
        return true;
    else
        return false;
}