具有intent的空异常启动活动

时间:2017-03-01 04:02:29

标签: java android android-intent

尝试使用null创建新活动时,我收到的异常等于Intent。基本上我的程序在我尝试创建这个新的Activity时终止,即将用户看到的屏幕切换为带有动画宝盒的屏幕(来自xml的png文件,当它们从MainActivity运行时起作用)。

以下是Android Studio收到的错误图片:

enter image description here

我正在运行vr4.0.4的设备上开发Vuzix M100应用程序。

以下是我正在运行的两项活动:

MainActivity.java

package com.rit.se.treasurehuntvuz;

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

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        try {
            Intent showTreasureIntent = new Intent(this, ShowTreasureActivity.class);
            startActivity(showTreasureIntent);
        }
        catch(Exception exception) {
            Log.e("MainActivity", exception.getMessage());
        }
    }
}

ShowTreasureActivity.java

package com.rit.se.treasurehuntvuz;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class ShowTreasureActivity extends AppCompatActivity {

    private ImageView treasureImage;
    private AnimationDrawable treasureAnimation;

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

        treasureImage = (ImageView)findViewById(R.id.treasureAnimationView);
        treasureImage.setBackgroundResource(R.drawable.anim_treasure);

        treasureAnimation = (AnimationDrawable) treasureImage.getBackground();
        treasureAnimation.setOneShot(true);
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus)
    {
        treasureAnimation.start();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
    }
}

1 个答案:

答案 0 :(得分:0)

感谢@Joel Duggan,结果发现我在清单文件中缺少一个条目。

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

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

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <activity android:name=".ShowTreasureActivity">
        </activity>


    </application>

</manifest>