我的Android应用在模拟器中运行时崩溃

时间:2019-06-30 05:29:15

标签: java android

我正在编写代码以使用户即使未使用SharedPreference退出应用程序,也可以保持登录状态。我添加了一个Splash类,它将在打开应用程序时运行。

在添加Splash类之前,该应用程序运行正常。

首先,我用以下代码定义了一个类First

class First
{
    static final String PREF_USER_NAME= "username";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    public static void setUserName(Context ctx, String userName)
    {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_USER_NAME, userName);
        editor.commit();
    }

    public static String getUserName(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
    }
}

然后,我想保存用户名,以便它可以保持登录状态。我猜First.setUserName(SignIn.this,Common.currentUser.getName())没有存储它。

这里是登录按钮的监听者:

btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ProgressDialog mDialog = new ProgressDialog(SignIn.this);

                mDialog.setMessage("Please Wait...");
                mDialog.show();
                table_user.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        //Check if user not exist in database
                        if(dataSnapshot.child(edtPhone.getText().toString()).exists()) {
                            //Get User Information
                            mDialog.dismiss();
                            User user = dataSnapshot.child(edtPhone.getText().toString()).getValue(User.class);
                            user.setPhone(edtPhone.getText().toString());
                            if (user.getPassword().equals(edtPassword.getText().toString())) {

                                First.setUserName(SignIn.this,Common.currentUser.getName());

                                //Toast.makeText(SignIn.this, "Sign In Successfully", Toast.LENGTH_SHORT).show();
                                Intent homeIntent= new Intent(SignIn.this,home.class);
                                Common.currentUser=user;
                                startActivity(homeIntent);

                                finish();
                            } else {
                                Toast.makeText(SignIn.this, "Sign In Failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else{
                            mDialog.dismiss();
                            Toast.makeText(SignIn.this,"User Not Exist in Database",Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        });

Splash类代码:

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle saveInstaceState)
    {

        super.onCreate(saveInstaceState);
        if(First.getUserName(Splash.this).length() == 0)
        {
           Intent a= new Intent(Splash.this,MainActivity.class);
           startActivity(a);
        }
        else
        {
            Intent a= new Intent(Splash.this,home.class);
            startActivity(a);
        }

    }

这是logcat:

2019-06-30 11:15:43.104 15040-15040/com.example.nilesh.internapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nilesh.internapp, PID: 15040
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nilesh.internapp/com.example.nilesh.internapp.Splash}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.nilesh.internapp/com.example.nilesh.internapp.MainActivity}; have you declared this activity in your AndroidManifest.xml?
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.nilesh.internapp/com.example.nilesh.internapp.MainActivity}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2049)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)
        at android.app.Activity.startActivityForResult(Activity.java:5173)
        at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
        at android.app.Activity.startActivityForResult(Activity.java:5131)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
        at android.app.Activity.startActivity(Activity.java:5502)
        at android.app.Activity.startActivity(Activity.java:5470)
        at com.example.nilesh.internapp.Splash.onCreate(Splash.java:20)
        at android.app.Activity.performCreate(Activity.java:7783)
        at android.app.Activity.performCreate(Activity.java:7772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7319) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

0 个答案:

没有答案