为什么我的程序在单击按钮时崩溃了?

时间:2021-11-20 20:46:57

I am trying to make a login and register app where the register details are stored on a database within android studio. Can you please help me as when i run my program the next page does not appear (shown code for one page that won't appear from main menu which is the register activity). If you also know how to make a sql database for a login and register in android studio would be much appreciated. Thanks

我正在尝试登录并注册应用程序,其中寄存器详细信息存储在android studio中的数据库中。你可以帮助我,因为当我运行我的程序时,下一页没有出现(显示一个页面的代码,不会出现在主菜单中,这是注册活动)。如果你也知道如何为登录做一个sql数据库并在android studio注册将非常感激。谢谢

Main Activity:

package com.example.emily.loginapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button Login, Register, Delete, Update;
    int status = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Login = (Button) findViewById(R.id.Login);
        Register = (Button) findViewById(R.id.Register);
        Delete = (Button) findViewById(R.id.Delete);
        Update = (Button) findViewById(R.id.Update);
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                status = 1;
                Bundle b = new Bundle();
                b.putInt("status", status);
                Intent i = new Intent("login_filter");
                i.putExtras(b);
                startActivity(i);
            }

        });
        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent("register_filter");
                startActivity(i);
            }
        });
        Update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                status = 2;
                Bundle b = new Bundle();
                b.putInt("status", status);
                Intent i = new Intent("login_filter");
                i.putExtras(b);
                startActivity(i);
            }
        });
        Delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        status= 3;
                Bundle b = new Bundle();
                b.putInt("status", status);
                Intent i = new Intent("login_filter");
                i.putExtras(b);
                startActivity(i);

            }
        });
    }
}

Register:

package com.example.emily.loginapp;                                                                                  

import android.content.Context;                                                                                      
import android.support.v7.app.AppCompatActivity;                                                                     
import android.view.View;                                                                                            
import android.os.Bundle;                                                                                            
import android.widget.EditText;                                                                                      
import android.widget.Button;                                                                                        
import android.widget.Toast;                                                                                         


public class RegisterActivity extends AppCompatActivity {                                                            

     EditText USER_NAME, USER_PASS, CON_PASS;                                                                        
    String user_name, user_pass,con_pass;                                                                            
Button REG;                                                                                                          
    Context ctx = this;                                                                                              

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

    USER_NAME = (EditText) findViewById(R.id.reg_user);                                                              
        USER_PASS = (EditText)findViewById(R.id.reg_pass);                                                           
        CON_PASS = (EditText) findViewById(R.id.con_pass);                                                           
REG = (Button) findViewById(R.id.user_reg);                                                                          
        REG.setOnClickListener(new View.OnClickListener() {                                                          
            @Override                                                                                                
            public void onClick(View v) {                                                                            
                user_name = USER_NAME.getText().toString();                                                          
                user_pass = USER_PASS.getText().toString();                                                          
                con_pass = CON_PASS.getText().toString();                                                            

                if (!(user_pass.equals(con_pass))){                                                                  
                    Toast.makeText(getBaseContext(),"Passwords are not matching", Toast.LENGTH_LONG).show();;        
                USER_NAME.setText("");                                                                               
                    USER_PASS.setText("");                                                                           
                    CON_PASS.setText("");                                                                            
                }else{                                                                                               
                    DatabaseOperations DB = new DatabaseOperations(ctx);                                             
                    DB.putInformation(DB, user_name, user_pass);                                                     
                    Toast.makeText(getBaseContext(),"Registration success", Toast.LENGTH_LONG).show();               
                    finish();                                                                                        

                }                                                                                                    
            }                                                                                                        
        });                                                                                                          

    }                                                                                                                

} 

LOGCAT:
03-09 22:33:25.504 2672-2672/com.example.emily.loginapp D/AndroidRuntime: Shutting down VM
03-09 22:33:25.537 2672-2672/com.example.emily.loginapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.emily.loginapp, PID: 2672
    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=register_filter }
      at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
      at android.app.Activity.startActivityForResult(Activity.java:3917)
      at android.app.Activity.startActivityForResult(Activity.java:3877)
      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
      at android.app.Activity.startActivity(Activity.java:4200)
      at android.app.Activity.startActivity(Activity.java:4168)
      at com.example.emily.loginapp.MainActivity$2.onClick(MainActivity.java:43)
      at android.view.View.performClick(View.java:5198)
      at android.view.View$PerformClick.run(View.java:21147)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-09 22:33:36.952 2672-2679/com.example.emily.loginapp W/art: Suspending all threads took: 29.841ms

manifest:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DeleteActivity"
            android:label="@string/title_activity_delete"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".UpdateActivity"
            android:label="@string/title_activity_update"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

4 个解决方案

#1


0  

You are using using an intent flag(register_filter) which is not available on the device thats why it is crashing the application...!

您正在使用意图标志(register_filter),该标志在设备上不可用,因为它崩溃了应用程序......!

The constructor of the Intent class should be call with the context of the activity/app and the activity on which you have to move(If you are not using an activity which is not available on the device)...!

应该使用activity / app的上下文和你必须移动的活动来调用Intent类的构造函数(如果你没有使用设备上没有的活动)......!

You should use the below code to access the RegisterActivity,

您应该使用以下代码访问RegisterActivity,

Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(i);

#2


0  

You need to create your intent using a class extending Activity. For example like this:

您需要使用扩展Activity的类来创建您的意图。例如这样:

Intent i = new Intent(this, SomeActivity.class);
startActivity(i);

where this is current Context and SomeActivity is the activity you want to start.

这是当前的Context,SomeActivity是您要启动的活动。

Take a look at the docs:

看看文档:

http://developer.android.com/training/basics/firstapp/starting-activity.html

#3


0  

To call a new Activity from the current Activity you need to to pass the context of the current activity and the "NewClass" which you want to open as parameters.

要从当前活动调用新活动,您需要传递当前活动的上下文和要作为参数打开的“NewClass”。

For Eg :

对于Eg:

    Intent i = new Intent(CurrentClassName.this, TheClassToBeOpened.class);
    startActivity(i);

#4


0  

IMHO using actions is overkill for what you want to achieve. You should just ude something like:

恕我直言,使用行动对你想要实现的目标来说是过度的。你应该只是这样的东西:

Intent i = new Intent(getContext(), RegistrationActivity.class);
startActivity(i);

(you can use this instead getContext() too).

(你也可以用它来代替getContext())。

If you do really want to use actions, then you must define the intent filter register_filter in your activity. Something along the lines:

如果您确实想要使用操作,则必须在活动中定义intent filter register_filter。一些事情:

<activity
    android:name=".LoginActivity"
    android:label="@string/title_activity_login"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="login_filter" />
    </intent-filter> 
</activity>

should do the trick.

应该做的伎俩。

For reference, please have a look at the documentation and this question.

如需参考,请查看文档和此问题。

#1


0  

You are using using an intent flag(register_filter) which is not available on the device thats why it is crashing the application...!

您正在使用意图标志(register_filter),该标志在设备上不可用,因为它崩溃了应用程序......!

The constructor of the Intent class should be call with the context of the activity/app and the activity on which you have to move(If you are not using an activity which is not available on the device)...!

应该使用activity / app的上下文和你必须移动的活动来调用Intent类的构造函数(如果你没有使用设备上没有的活动)......!

You should use the below code to access the RegisterActivity,

您应该使用以下代码访问RegisterActivity,

Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(i);

#2


0  

You need to create your intent using a class extending Activity. For example like this:

您需要使用扩展Activity的类来创建您的意图。例如这样:

Intent i = new Intent(this, SomeActivity.class);
startActivity(i);

where this is current Context and SomeActivity is the activity you want to start.

这是当前的Context,SomeActivity是您要启动的活动。

Take a look at the docs:

看看文档:

http://developer.android.com/training/basics/firstapp/starting-activity.html

#3


0  

To call a new Activity from the current Activity you need to to pass the context of the current activity and the "NewClass" which you want to open as parameters.

要从当前活动调用新活动,您需要传递当前活动的上下文和要作为参数打开的“NewClass”。

For Eg :

对于Eg:

    Intent i = new Intent(CurrentClassName.this, TheClassToBeOpened.class);
    startActivity(i);

#4


0  

IMHO using actions is overkill for what you want to achieve. You should just ude something like:

恕我直言,使用行动对你想要实现的目标来说是过度的。你应该只是这样的东西:

Intent i = new Intent(getContext(), RegistrationActivity.class);
startActivity(i);

(you can use this instead getContext() too).

(你也可以用它来代替getContext())。

If you do really want to use actions, then you must define the intent filter register_filter in your activity. Something along the lines:

如果您确实想要使用操作,则必须在活动中定义intent filter register_filter。一些事情:

<activity
    android:name=".LoginActivity"
    android:label="@string/title_activity_login"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="login_filter" />
    </intent-filter> 
</activity>

should do the trick.

应该做的伎俩。

For reference, please have a look at the documentation and this question.

如需参考,请查看文档和此问题。