应用程序未从LoginActivity切换到MainActivity

时间:2015-11-07 08:14:07

标签: android android-intent android-styles

Main Activity.Class

package com.developer.akshay.youmego;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback {
    Button button_offer_ride;
    Button button_find_ride;
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //map start
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        //map stop



        button_find_ride=(Button) findViewById(R.id.btn_findride);
        button_offer_ride=(Button) findViewById(R.id.btn_offerride);
        button_find_ride.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten_onClick_findride=new Intent(MainActivity.this,FindActivity.class);
                startActivity(inten_onClick_findride);
            }
        });

        button_offer_ride.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten_onClick_Offerride=new Intent(MainActivity.this,OfferActivity.class);
                startActivity(inten_onClick_Offerride);
            }
        });


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @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);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camara) {
            Toast.makeText(MainActivity.this, "Profile page will created soon..", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_gallery) {
            Toast.makeText(MainActivity.this, "Offer Ride page", Toast.LENGTH_SHORT).show();
            Intent intent= new Intent(MainActivity.this,OfferActivity.class);
            startActivity(intent);

        } else if (id == R.id.nav_slideshow) {

            Toast.makeText(MainActivity.this, "Find Ride page", Toast.LENGTH_SHORT).show();
            Intent intent= new Intent(MainActivity.this,FindActivity.class);
            startActivity(intent);

        } else if (id == R.id.nav_manage) {
            Toast.makeText(MainActivity.this, "Tool Page will created Soon", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.nav_share) {

            Toast.makeText(MainActivity.this, "Share page will created Soon..", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_send) {

            Toast.makeText(MainActivity.this, "Send page will created Soon...", Toast.LENGTH_SHORT).show();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Jaipur and move the camera
        LatLng jaipur = new LatLng(26, 75.80);
        LatLng bikaner= new LatLng(28.0167, 73.3119);
        LatLng bangalore=new LatLng(12.9667, 77.5667);
        LatLng delhi=new LatLng(28.6100, 77.2300);
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        mMap.setMyLocationEnabled(true);
        mMap.addMarker(new MarkerOptions().position(jaipur).title("Cars available in Jaipur"))
                .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.caricon));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(jaipur));
        mMap.addMarker(new MarkerOptions().position(bikaner).title("Cars available in Bikaner"))
                .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.caricon));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(bikaner));
        mMap.addMarker(new MarkerOptions().position(bangalore).title("Cars available in Bangalore"))
                .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.caricon));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(bangalore));
        mMap.addMarker(new MarkerOptions().position(delhi).title("Cars available in Delhi"))
                .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.caricon));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(delhi));

    }
}

Splash.class

package com.developer.akshay.youmego;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.parse.ParseUser;

public class Splash extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                if (ParseUser.getCurrentUser() != null) {
                    // Start an intent for the logged in activity
                    startActivity(new Intent(Splash.this, LoginActivity.class));
                } else {
                    // Start and intent for the logged out activity
                    startActivity(new Intent(Splash.this, MainActivity.class));
                }
            }
        }, 3000);
    }

}

LoginActivity.class

package com.dev.ak.youmego;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.sourcey.YouMeGo.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    private static final int REQUEST_SIGNUP = 0;

    @InjectView(R.id.input_username) EditText _usernameText;
    @InjectView(R.id.input_password) EditText _passwordText;
    @InjectView(R.id.btn_login) Button _loginButton;
    @InjectView(R.id.link_signup) TextView _signupLink;

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


        _loginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                login();
            }
        });

        _signupLink.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Start the Signup activity
                Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
                startActivityForResult(intent, REQUEST_SIGNUP);
            }
        });
    }

    public void login() {
        Log.d(TAG, "Login");

        if (!validate()) {
            onLoginFailed();
            return;
        }

        _loginButton.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
                R.style.AppTheme_Dark_Dialog);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Authenticating...");
        progressDialog.show();
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        onLoginSuccess();
                        progressDialog.dismiss();
                    }
                }, 3000);
    }

    void onLoginSuccess() {

        // TODO: Authentication Source
        ParseUser.logInInBackground(_usernameText.getText().toString(),
                _passwordText.getText().toString(), new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if (parseUser != null) {
                            moveTaskToBack(false);
                            Toast.makeText(LoginActivity.this, "Logged in Successfully!!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
                                    Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        } else {

                            Toast.makeText(LoginActivity.this, "\n Enter Correct User Crediantials.",
                                    Toast.LENGTH_LONG).show();
                            _loginButton.setEnabled(true);
                        }
                    }
                });
    }



    void onLoginFailed(){
        _passwordText.getText().clear();
        _usernameText.getText().clear();
        Toast.makeText(LoginActivity.this, "Login Failed!!", Toast.LENGTH_SHORT).show();
    }

    public boolean validate() {
        boolean valid = true;

        String username = _usernameText.getText().toString();
        String password = _passwordText.getText().toString();

        if (username.isEmpty()) {
            _usernameText.setError("enter a valid username");
            Toast.makeText(LoginActivity.this, "Enter a valid username!", Toast.LENGTH_SHORT).show();
            valid = false;

        } else {
            _usernameText.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            _passwordText.setError("between 4 and 10 alphanumeric characters");
            Toast.makeText(LoginActivity.this, "Enter a valid Password!", Toast.LENGTH_SHORT).show();
            valid = false;
        } else {
            _passwordText.setError(null);
        }

        return valid;
    }
}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.developer.akshay.youmego" >
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



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


    <application
        android:name=".ParseApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <activity
            android:name=".Splash"
            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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
        <activity android:name=".MapsActivity"/>
        <activity android:name=".SignupActivity" />
        <activity android:name=".LoginActivity" >
        </activity>
    </application>

</manifest>

风格

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppTheme.Dark.Dialog" parent="Theme.AppCompat.Dialog">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
        <item name="android:background">@color/colorPrimary</item>

    </style>


</resources>

logcat的

11-07 13:38:31.231 1350-1363/? D/SystemAdController: Package:com.developer.akshay.youmego is not in the list
11-07 13:38:31.231 984-1443/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.developer.akshay.youmego/.Splash bnds=[378,657][514,793]} from pid 1321
11-07 13:38:31.291 984-1318/? I/ActivityManager: Start proc com.developer.akshay.youmego for activity com.developer.akshay.youmego/.Splash: pid=31250 uid=10135 gids={50135, 3003, 1028, 1015}
11-07 13:38:31.361 1489-1644/? D/PowerKeeperEventLogManager: notifyForegroundCompomentChanged ComponentInfo{com.developer.akshay.youmego/com.developer.akshay.youmego.Splash}
11-07 13:38:31.371 31250-31250/? D/ActivityThread: handleBindApplication:com.developer.akshay.youmego
11-07 13:38:31.861 984-997/? I/ActivityManager: Displayed com.developer.akshay.youmego/.Splash: +578ms (total +15m38s366ms)
11-07 13:38:31.861 984-997/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{4386c210 u0 com.developer.akshay.youmego/.Splash t75} time:27751790
11-07 13:38:34.661 1350-1364/? D/SystemAdController: Activity:ActivityInfo{42afa560 com.developer.akshay.youmego.LoginActivity}is not the default one
11-07 13:38:34.661 1350-1364/? D/SystemAdController: Package:com.developer.akshay.youmego is not in the list
11-07 13:38:34.661 984-1441/? I/ActivityManager: START u0 {cmp=com.developer.akshay.youmego/.LoginActivity} from pid 31250
11-07 13:38:34.671 1489-1644/? D/PowerKeeperEventLogManager: notifyForegroundCompomentChanged ComponentInfo{com.developer.akshay.youmego/com.developer.akshay.youmego.LoginActivity}
11-07 13:38:34.911 984-997/? I/ActivityManager: Displayed com.developer.akshay.youmego/.LoginActivity: +239ms
11-07 13:38:35.241 984-997/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{436e6978 u0 com.developer.akshay.youmego/.LoginActivity t75} time:27755173
11-07 13:38:57.301 1260-1621/? I/XiaomiFirewall: firewall pkgName:com.developer.akshay.youmego, result:0
11-07 13:38:59.021 31250-31250/? W/Toast: From com.developer.akshay.youmego, go ahead.
11-07 13:38:59.031 1350-27999/? D/SystemAdController: Activity:ActivityInfo{42b593f8 com.developer.akshay.youmego.MainActivity}is not the default one
11-07 13:38:59.031 1350-27999/? D/SystemAdController: Package:com.developer.akshay.youmego is not in the list
11-07 13:38:59.031 1350-1364/? D/SystemAdController: activity destroyed:ActivityInfo{42b5a2a0 com.developer.akshay.youmego.Splash} not in mAllAppsInLauncher
11-07 13:38:59.031 984-1517/? I/ActivityManager: START u0 {flg=0x10008000 cmp=com.developer.akshay.youmego/.MainActivity} from pid 31250
11-07 13:38:59.081 1489-1644/? D/PowerKeeperEventLogManager: notifyForegroundCompomentChanged ComponentInfo{com.developer.akshay.youmego/com.developer.akshay.youmego.MainActivity}
11-07 13:38:59.561 31250-31250/? W/ContextImpl: Failed to ensure directory: /storage/sdcard1/Android/data/com.developer.akshay.youmego/cache
11-07 13:38:59.741 1260-1621/? I/XiaomiFirewall: firewall pkgName:com.developer.akshay.youmego, result:0
11-07 13:38:59.751 1744-2622/? I/KLO_Stability: [am_crash][1446883739]:[31250,0,com.developer.akshay.youmego,13155910,java.lang.IllegalStateException,This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.,AppCompatDelegateImplV7.java,199]
11-07 13:38:59.751 31250-31250/? E/AndroidRuntime: Process: com.developer.akshay.youmego, PID: 31250
11-07 13:38:59.751 31250-31250/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.akshay.youmego/com.developer.akshay.youmego.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
11-07 13:38:59.751 31250-31250/? E/AndroidRuntime:     at com.developer.akshay.youmego.MainActivity.onCreate(MainActivity.java:35)
11-07 13:38:59.761 1307-1507/? W/KloServer: Aborted broadcast does not supprt for: com.developer.akshay.youmego
11-07 13:38:59.771 984-1041/? W/ActivityManager:   Force finishing activity com.developer.akshay.youmego/.MainActivity
11-07 13:38:59.851 984-1444/? I/ActivityManager: Process com.developer.akshay.youmego (pid 31250) has died.
11-07 13:38:59.871 984-1048/? W/InputDispatcher: channel '433d2eb0 com.developer.akshay.youmego/com.developer.akshay.youmego.LoginActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
11-07 13:38:59.871 984-1048/? E/InputDispatcher: channel '433d2eb0 com.developer.akshay.youmego/com.developer.akshay.youmego.LoginActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-07 13:38:59.871 984-1042/? W/InputDispatcher: Attempted to unregister already unregistered input channel '433d2eb0 com.developer.akshay.youmego/com.developer.akshay.youmego.LoginActivity (server)'
11-07 13:38:59.871 984-1042/? I/WindowState: WIN DEATH: Window{433d2eb0 u0 com.developer.akshay.youmego/com.developer.akshay.youmego.LoginActivity}
11-07 13:39:01.021 984-984/? W/NotificationService: Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@4354a7b0 in package com.developer.akshay.youmego

以下是我的应用的完整代码。 其次,如果我将MainActivity作为启动器活动。然后MainActivity运行得非常好,否则当我将SplashActivity作为启动器活动时,它会给出一个你可以在logcat中看到的错误。

1 个答案:

答案 0 :(得分:0)

更改清单(theme标记)中的<application>

android:theme="@style/AppTheme"

为:

android:theme="@style/AppTheme.NoActionBar"

或将theme属性添加到MainActivity的{​​{1}}标记。

在此更改系统之前,您需要<activity>中的ActionBar