我有以下Android Unity 3D项目文件,它在运行时使用java.lang.RuntimeException编译但失败:无法启动活动ComponentInfo java.lang.nullpointerexception。
这是UnityPlayerNavtiveActivity.java
package com.sniper.game;
import com.unity3d.player.*;
import android.app.NativeActivity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class UnityPlayerNativeActivity extends NativeActivity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
@Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
// Quit Unity
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
@Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
@Override protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
@Override public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
}
这是UnityPlayerActivity.java:
package com.sniper.game;
import com.unity3d.player.*;
/**
* @deprecated Use UnityPlayerNativeActivity instead.
*/
public class UnityPlayerActivity extends UnityPlayerNativeActivity { }
这是UnityPlayerProxyActivity.java:
package com.sniper.game;
import com.unity3d.player.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
/**
* @deprecated Use UnityPlayerNativeActivity instead.
*/
public class UnityPlayerProxyActivity extends Activity
{
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, com.sniper.game.UnityPlayerNativeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Bundle extras = getIntent().getExtras();
if (extras != null)
intent.putExtras(extras);
startActivity(intent);
}
}
这是AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="5" android:versionName="2.0" package="com.sniper.game" android:installLocation="preferExternal">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
<!-- Google Mobile Ads Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<!-- meta-data tag for Google Play services -->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerProxyActivity">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerActivity">
</activity>
<activity android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerNativeActivity">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:name="com.unity3d.player.VideoPlayer">
</activity>
<!-- Google Mobile Ads Activity -->
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:name="com.google.android.gms.ads.AdActivity">
</activity>
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
以下是几小时后从上面的屏幕截图中发现的完整日志跟踪:
05-15 14:14:03.840: D/AndroidRuntime(1583): Shutting down VM
05-15 14:14:03.840: W/dalvikvm(1583): threadid=1: thread exiting with uncaught exception (group=0xb1ac2b90)
05-15 14:14:03.850: E/AndroidRuntime(1583): FATAL EXCEPTION: main
05-15 14:14:03.850: E/AndroidRuntime(1583): Process: com.sniper.game, PID: 1583
05-15 14:14:03.850: E/AndroidRuntime(1583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sniper.game/com.sniper.game.UnityPlayerNativeActivity}: java.lang.NullPointerException
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Looper.loop(Looper.java:137)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-15 14:14:03.850: E/AndroidRuntime(1583): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 14:14:03.850: E/AndroidRuntime(1583): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-15 14:14:03.850: E/AndroidRuntime(1583): at dalvik.system.NativeStart.main(Native Method)
05-15 14:14:03.850: E/AndroidRuntime(1583): Caused by: java.lang.NullPointerException
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Parcel.readException(Parcel.java:1467)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Parcel.readException(Parcel.java:1415)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.storage.IMountService$Stub$Proxy.mkdirs(IMountService.java:750)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.ensureDirsExistOrFilter(ContextImpl.java:2160)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.getObbDirs(ContextImpl.java:874)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.getObbDir(ContextImpl.java:863)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.content.ContextWrapper.getObbDir(ContextWrapper.java:220)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.NativeActivity.onCreate(NativeActivity.java:177)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.sniper.game.UnityPlayerNativeActivity.onCreate(UnityPlayerNativeActivity.java:22)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.Activity.performCreate(Activity.java:5243)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-15 14:14:03.850: E/AndroidRuntime(1583): ... 11 more
我需要帮助让这个Unity3D Android游戏在模拟器上运行,然后下载并在实际设备上运行。