简体   繁体   中英

App crashing after using intent to open a new activity after login

I have been trying to get my app to redirect to a new activity after login, I have tried almost everything to no avail. I have also checked all related answers here on the forums but nothing seems to work. Any help would be greatly appreciated.

The Login Activity Code:

@Override
public void onSuccess(@NonNull final Credentials credentials) {
Intent i = new Intent(MainActivity.this, testactivity.class);
startActivity(i);

Manifest:

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

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

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

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

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="@string/auth0_domain"
                    android:pathPrefix="/android/com.auth0.samples/callback"
                    android:scheme="demo" />
            </intent-filter>
        </activity>
        <activity android:name="materialtabs.activity.SimpleTabsActivity" />
        <activity android:name=".testactivity"></activity>
    </application>
</manifest>

Test.java:

     package com.auth0.samples;

 import android.support.design.widget.TabLayout;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.Toolbar;

 import com.auth0.samples.fragments.OneFragment;
 import com.auth0.samples.fragments.ThreeFragment;
 import com.auth0.samples.fragments.TwoFragment;

 import java.util.ArrayList;
 import java.util.List;

 public class test extends AppCompatActivity {

     private Toolbar toolbar;
     private TabLayout tabLayout;
     private ViewPager viewPager;

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

         toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);

         viewPager = (ViewPager) findViewById(R.id.viewpager);
         setupViewPager(viewPager);

         tabLayout = (TabLayout) findViewById(R.id.tabs);
         tabLayout.setupWithViewPager(viewPager);
     }

     private void setupViewPager(ViewPager viewPager) {
         ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
         adapter.addFragment(new OneFragment(), "Notices");
         adapter.addFragment(new TwoFragment(), "Timetable");
         adapter.addFragment(new ThreeFragment(), "Homework");
         viewPager.setAdapter(adapter);
     }

     class ViewPagerAdapter extends FragmentPagerAdapter {
         private final List<Fragment> mFragmentList = new ArrayList<>();
         private final List<String> mFragmentTitleList = new ArrayList<>();

         public ViewPagerAdapter(FragmentManager manager) {
             super(manager);
         }

         @Override
         public Fragment getItem(int position) {
             return mFragmentList.get(position);
         }

         @Override
         public int getCount() {
             return mFragmentList.size();
         }

         public void addFragment(Fragment fragment, String title) {
             mFragmentList.add(fragment);
             mFragmentTitleList.add(title);
         }

         @Override
         public CharSequence getPageTitle(int position) {
             return mFragmentTitleList.get(position);
         }
     }
 }

Upon further trouble shooting I have noticed that the app will redirect to a blank activity so there must be something that is being called in test.java that is messing things up.

Welcome, a few tips first: post the error logs and more related code. In this example, under what context is the onSuccess(...) called.

Now to answer your question, I think it fails because your intent code is in onSuccess call. Have you tried debugging by moving the startActivity to outside of the onSuccess? Let's try the easiest test, simply move that intent code to the onCreate of your MainActivity. This should make it so that your mainactivity immediately go to testactivity. If your app does not do this, it means something is wrong in the testactivity.java file. If it does, it means your onsuccess() is problem.

By doing simple debugging such as above, you can easily isolate the real issue, which leads to solving the problem. Start debugging in small incremental steps! Also if you haven't already , refer here for more details on using intents to start activity.

Edit: thanks I see the new code, so your test class is called test.java instead of testactivity.java? Then in your intent, the target should be test.class.

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