Android开发:应用程序在调用另一个活动后崩溃

时间:2017-07-22 17:28:54

标签: android crash splash-screen

我正在尝试调用另一种方法,我的应用程序崩溃了。我尝试了所有可能找到的解决方案,但没有任何效果。

Android Manifest:

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

<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: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=".SplashScreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    </activity>

</application>

</manifest>

SplashScreen(callinf另一种方法):     package com.dmz.humiliation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;

public class SplashScreen extends Activity
{
private static int SPLASH_TIME_OUT = 5000;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    /*
    new Handler().postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            Toast.makeText(SplashScreen.this,"1", 
Toast.LENGTH_SHORT).show();
            Intent i = new Intent(SplashScreen.this, NameAge.class);
            startActivity(i);
            SplashScreen.this.finish();
        }
    }, SPLASH_TIME_OUT);
    */

    Timer timer = new Timer();
    timer.schedule(new TimerTask()
    {

        public void run()
        {
            Toast.makeText(SplashScreen.this,"1", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(SplashScreen.this, NameAge.class);
            startActivity(i);
            SplashScreen.this.finish();
        }

    }, 3000);

}

}

NameAge(启动画面后调用的方法) package com.dmz.humiliation;

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

public class NameAge extends AppCompatActivity implements 
View.OnClickListener
{

EditText nameText, ageText;
Button saveButton;
SharedPreferences sPref;

final String NAME = "saved_text";
final String AGE = "saved_text";

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

    nameText = (EditText)findViewById(R.id.nameText);
    ageText = (EditText)findViewById(R.id.ageText);
    saveButton = (Button)findViewById(R.id.saveButton);
    saveButton.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.saveButton:
            saveText();
            Intent i = new Intent(NameAge.this, MainActivity.class);
            startActivity(i);
            finish();
            break;
    }
}

private void saveText()
{
    sPref = getSharedPreferences("MyPref", MODE_PRIVATE);
    SharedPreferences.Editor ed = sPref.edit();
    ed.putString(NAME, nameText.getText().toString());
    ed.putString(AGE, ageText.getText().toString());
    ed.commit();
}

public String getNAME()
{
    sPref = getSharedPreferences("MyPref", MODE_PRIVATE);
    String name = sPref.getString("MyPref", NAME);
    return name;
}

    public String getAge()
    {
        sPref = getSharedPreferences("MyPref", MODE_PRIVATE);
        String age = sPref.getString("MyPref", AGE);
        return age;    
    }
}

希望它能帮助你给我建议。

2 个答案:

答案 0 :(得分:1)

您需要在清单

中注册NameAge个活动
<activity android:name=".NameAge"/>

提示:将MainActivity更改为DEFAULT而不是LAUNCHER,以便在您的应用中保留一个入口点

答案 1 :(得分:1)

问题是您没有在清单中声明您的NameAge活动。您再次将两个活动声明为MainIntent,并在Launcher类别中声明。

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

<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: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=".SplashScreen"
android:label="@string/app_name"
//start /* android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>*///end
</activity>

</application>

</manifest>

Try this:

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

<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=".SplashScreen"
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=".MainActivity"
android:label="@string/app_name"
</activity>
<activity
android:name=".NameAge"
android:label="@string/app_name"
</activity>
</application>
</manifest>