Android 无法实例化应用程序,我已添加权限&模拟器具有外部存储
我看不出是什么导致了这个错误。我只在emulator中尝试过应用程序,但emulator有外部存储,我正在尝试将图像写入外部存储,然后在gallery中打开它 当我点击按钮时,它根本不起作用 这是我的日志:Android 无法实例化应用程序,我已添加权限&模拟器具有外部存储,android,android-emulator,Android,Android Emulator,我看不出是什么导致了这个错误。我只在emulator中尝试过应用程序,但emulator有外部存储,我正在尝试将图像写入外部存储,然后在gallery中打开它 当我点击按钮时,它根本不起作用 这是我的日志: 01-01 12:03:21.963: E/AndroidRuntime(693): FATAL EXCEPTION: main 01-01 12:03:21.963: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to
01-01 12:03:21.963: E/AndroidRuntime(693): FATAL EXCEPTION: main
01-01 12:03:21.963: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.os.Looper.loop(Looper.java:137)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-01 12:03:21.963: E/AndroidRuntime(693): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 12:03:21.963: E/AndroidRuntime(693): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 12:03:21.963: E/AndroidRuntime(693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 12:03:21.963: E/AndroidRuntime(693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 12:03:21.963: E/AndroidRuntime(693): at dalvik.system.NativeStart.main(Native Method)
01-01 12:03:21.963: E/AndroidRuntime(693): Caused by: java.lang.NullPointerException
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
01-01 12:03:21.963: E/AndroidRuntime(693): at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
代码-括号似乎有点乱,但你得到了jist:
package com.xProGames.pythonchallengesforbeginners.challenges;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import com.xProGames.pythonchallengesforbeginners.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Challenge1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.challenge1_layout);
Button hint = (Button) findViewById(R.id.hint_button_1);
Button answer = (Button) findViewById(R.id.solution_button_1);
hint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CreateAlert();
}
});
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Converts the file to a bitmap
Drawable myDrawable = getResources().getDrawable(
R.drawable.challenge1_solution);
Bitmap challenge1_solution = ((BitmapDrawable) myDrawable)
.getBitmap();
// Saves the file to internal storage
SaveImage(challenge1_solution);
// Open gallery & image
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
});
}
private void CreateAlert() {
new AlertDialog.Builder(this).setTitle("Hint")
.setMessage("Use the print() statement").show();
}
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "challenge_1.png";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xProGames.pythonchallengesforbeginners"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.xProGames.pythonchallengesforbeginners.SplashScreen"
android:label="Welcome!" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="Choose a challenge" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 1 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge1"
android:label="Challenge 1" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 2 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge2"
android:label="Challenge 2" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 3 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge3"
android:label="Challenge 3" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 4 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge4"
android:label="Challenge 4" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE4" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 5 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge5"
android:label="Challenge 5" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE5" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 6 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge6"
android:label="Challenge 6" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE6" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 7 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge7"
android:label="Challenge 7" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE7" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 8 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge8"
android:label="Challenge 8" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE8" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 9 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge9"
android:label="Challenge 9" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE9" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 10 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge10"
android:label="Challenge 10" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE10" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 11 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge11"
android:label="Challenge 11" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE11" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 12 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge12"
android:label="Challenge 12" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE12" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 13 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge13"
android:label="Challenge 13" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE13" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 14 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge14"
android:label="Challenge 14" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE14" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 15 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge15"
android:label="Challenge 15" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE15" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 16 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge16"
android:label="Challenge 16" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE16" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 17 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge17"
android:label="Challenge 17" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE17" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 18 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge18"
android:label="Challenge 18" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE18" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 19 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge19"
android:label="Challenge 19" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE19" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 20 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge20"
android:label="Challenge 20" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE20" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 21 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge21"
android:label="Challenge 21" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE21" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 22 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge22"
android:label="Challenge 22" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE22" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 23 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge23"
android:label="Challenge 23" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE23" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 24 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge24"
android:label="Challenge 24" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE24" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Challenge 25 -->
<activity
android:name="com.xProGames.pythonchallengesforbeginners.challenges.Challenge25"
android:label="Challenge 25" >
<intent-filter>
<action android:name="com.xProGames.pythonchallengesforbeginners.challenges.CHALLENGE25" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
您是否使用过任何骨架应用程序类??不,我将用代码更新问题。显示清单文件您是否检查了Java Build Path in Properties中是否选中了所需的复选框?否,需要选中哪些复选框?