应用程序在从一个活动转移到另一个活动时已停止

时间:2015-11-06 06:31:40

标签: java android xml android-intent

我的项目有两项活动。 1)登录活动2)欢迎

首先,在实施任何登录验证之前,我试图从一个活动转移到另一个活动。我以前用了很多次。但对于这个项目,我无法找到我的应用程序在转换时停止的原因。

转换说明:当有人点击登录按钮时,会将他带到欢迎画面。 (实践中我没有实施任何认证)

的AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".Launching"/>

        <activity android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".Launching" />

    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="@bool/fitsSystemWindows">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_kitkat_height"
            android:background="?colorPrimary"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_lollipop_height"
            android:background="?colorPrimaryDark"/>

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/status_bar_margin_top">

        <TextView
            android:id="@+id/txtusername"
            android:layout_marginTop="32pt"
            android:textSize="20dp"
            android:textColor="@color/md_black_1000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Username" />

        <EditText
            android:layout_marginTop="45pt"
            android:textSize="20dp"
            android:textColor="@color/md_black_1000"
            android:id="@+id/username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtusername"
            android:inputType="text" />

        <TextView
            android:id="@+id/txtpassword"
            android:layout_marginTop="69pt"
            android:textSize="20dp"
            android:textColor="@color/md_black_1000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/username"
            android:text="@string/Password" />

        <EditText
            android:id="@+id/password"
            android:layout_marginTop="85pt"
            android:textSize="20dp"
            android:textColor="@color/md_black_1000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtpassword"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/login"
            android:layout_marginTop="109pt"
            android:textSize="20dp"
            android:textColor="@color/md_white_1000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/password"
            android:text="@string/LoginBtn" />

        <Button
            android:id="@+id/signup"

            android:layout_marginTop="130pt"
            android:textSize="20dp"
            android:textColor="@color/md_white_1000"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/login"
            android:text="@string/SignupBtn" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ToolbarTheme" />

    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

launching.xml(带导航标题和navigation_menu.xml的欢迎屏幕)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="@bool/fitsSystemWindows">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_kitkat_height"
            android:background="?colorPrimary"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/status_bar_lollipop_height"
            android:background="?colorPrimaryDark"/>

    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/status_bar_margin_top">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Inbox"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            android:textColor="@color/md_text" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ToolbarTheme" />

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="@bool/fitsSystemWindows"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/navigation_drawer_menu"
        app:theme="@style/NavigationViewTheme" />

</android.support.v4.widget.DrawerLayout>

MainActivity.java

package world.yakuza.krishnavanshi.dev06_11_2015;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button login;
    Toolbar toolbar;

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

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

        login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                Intent intent = new Intent(getApplicationContext(),Launching.class);
                startActivity(intent);
        }
    }
}

Launching.java

package world.yakuza.krishnavanshi.dev06_11_2015;

/**
 * Created by Krishnavanshi on 06-11-2015.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Launching extends AppCompatActivity {

    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ActionBar actionBar;
    TextView textView;

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

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

        actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);

        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
        if (navigationView != null) {
            setupNavigationDrawerContent(navigationView);
        }

        setupNavigationDrawerContent(navigationView);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupNavigationDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        textView = (TextView) findViewById(R.id.textView);
                        switch (menuItem.getItemId()) {
                            case R.id.item_navigation_drawer_about:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_offers:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_menu:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_gallery:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_booktable:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_review:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                            case R.id.item_navigation_drawer_settings:
                                menuItem.setChecked(true);
                                textView.setText(menuItem.getTitle());
                                Toast.makeText(Launching.this, "MainActivity " + menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                                drawerLayout.closeDrawer(GravityCompat.START);
                                Intent intent = new Intent(Launching.this, SettingsActivity.class);
                                startActivity(intent);
                                return true;
                            case R.id.item_navigation_drawer_help_and_feedback:
                                menuItem.setChecked(true);
                                Toast.makeText(Launching.this, menuItem.getTitle().toString(), Toast.LENGTH_SHORT).show();
                                drawerLayout.closeDrawer(GravityCompat.START);
                                return true;
                        }
                        return true;
                    }
                });
    }
}

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "world.yakuza.krishnavanshi.dev06_11_2015"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
}

Login screen

second activity screen

logcat的

11-06 12:09:42.480        0-934/? I/qemu-props﹕ connected to 'boot-properties' qemud service.
11-06 12:09:39.180      928-928/? W/auditd﹕ type=2000 audit(0.0:1): initialized
11-06 12:09:39.430      928-928/? I/auditd﹕ type=1403 audit(0.0:2): policy loaded auid=4294967295 ses=4294967295
11-06 12:09:39.430      928-928/? W/auditd﹕ type=1404 audit(0.0:3): enforcing=1 old_enforcing=0 auid=4294967295 ses=4294967295
11-06 12:09:42.480        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.481        0-934/? I/qemu-props﹕ received: dalvik.vm.heapsize=64m
11-06 12:09:42.483        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.483        0-934/? I/qemu-props﹕ received: qemu.sf.lcd_density=320
11-06 12:09:42.484        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.485        0-934/? I/qemu-props﹕ received: qemu.hw.mainkeys=0
11-06 12:09:42.486        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.486        0-934/? I/qemu-props﹕ received: qemu.sf.fake_camera=none
11-06 12:09:42.488        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.488        0-934/? I/qemu-props﹕ received: ro.opengles.version=131072
11-06 12:09:42.504        0-934/? I/qemu-props﹕ receiving..
11-06 12:09:42.505        0-934/? I/qemu-props﹕ exiting (5 properties set).
11-06 12:09:42.666        0-945/? I/installd﹕ installd firing up
11-06 12:09:42.986        0-940/? I/Netd﹕ Netd 1.0 starting
11-06 12:09:43.042        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/default/optimistic_dad: No such file or directory
11-06 12:09:43.042        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/eth0/optimistic_dad: No such file or directory
11-06 12:09:43.043        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/lo/optimistic_dad: No such file or directory
11-06 12:09:43.043        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/sit0/optimistic_dad: No such file or directory
11-06 12:09:43.043        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/default/use_optimistic: No such file or directory
11-06 12:09:43.045        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/eth0/use_optimistic: No such file or directory
11-06 12:09:43.045        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/lo/use_optimistic: No such file or directory
11-06 12:09:43.046        0-940/? E/Netd﹕ Failed to open /proc/sys/net/ipv6/conf/sit0/use_optimistic: No such file or directory
    --------- beginning of system

3 个答案:

答案 0 :(得分:1)

您很可能为Launching活动设置了错误的布局:

setContentView(R.layout.activity_main);

它可能不是activity_main

答案 1 :(得分:1)

Launching.java

替换此行

setContentView(R.layout.activity_main);

setContentView(R.layout.launching);

答案 2 :(得分:1)

使用

设置错误的布局

setContentView(R.layout.activity_main)

取代

setContentView(R.layout.launching)

这样活动就无法获得布局的ID,因此它会崩溃