2017-03-01 93 views
0

当我试图使用Intent尝试创建新活动时,我收到的例外等于null。基本上,当我尝试创建这个新的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() { 
    } 
} 
+3

异常显示为空的事实看起来更像调试器问题,而不是代码问题。代码看起来很好。 catch块中的日志语句是否显示Logcat中的任何内容?当你点击后退按钮时什么都不会发生?您是否将ShowTreasureActivity添加到您的清单中? –

+0

当我点击后退按钮时程序崩溃。并没有从日志中。而且它是清单文件! – visc

+0

你是否来自同一活动,你是通过回来按? –

回答

0

感谢@Joel杜根,原来我是缺乏在清单中的条目文件。

<?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>