0

シナリオは次のとおりです。

イメージ ファイルがローカル パスで見つかった場合は、それが使用されます。それ以外の場合は、イメージ ファイルが最初にダウンロードされます。画像は ID と名前でマークされます。

事前に感謝します。英語がほとんどないため、私の文章が短くて申し訳ありません。

FileOutputStream fos;
FileInputStream fis; 
        try {
            fis = openFileInput(Integer.toString(id)+"_"+getName());
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        // exist
        if(fis!=null){
            this.setImage(BitmapFactory.decodeStream(fis));
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        // not exist
        }else{
            this.setImage(getBITMAP.BitmapUrl(image));
            try {
                fos = openFileOutput(Integer.toString(id)+"_"+getName(), Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            getImage().compress(Bitmap.CompressFormat.PNG, 100, fos);
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

PD: エラーがアプリケーションのクラッシュで発生することを忘れていました:The application ****** has stopped unexpectedly. Please try again.

これは LogCat です。

09-06 09:03:14.253: D/dalvikvm(472): GC freed 404 objects / 23688 bytes in 54ms
09-06 09:03:15.214: I/global(472): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
09-06 09:03:15.233: D/AndroidRuntime(472): Shutting down VM
09-06 09:03:15.233: W/dalvikvm(472): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 09:03:15.233: E/AndroidRuntime(472): Uncaught handler: thread main exiting due to uncaught exception
09-06 09:03:15.243: E/AndroidRuntime(472): java.lang.RuntimeException: Unable to start activity ComponentInfo{match.now.play/match.now.play.MatchNowGames}: java.lang.NullPointerException
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.os.Looper.loop(Looper.java:123)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.main(ActivityThread.java:4363)
09-06 09:03:15.243: E/AndroidRuntime(472):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 09:03:15.243: E/AndroidRuntime(472):  at java.lang.reflect.Method.invoke(Method.java:521)
09-06 09:03:15.243: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-06 09:03:15.243: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-06 09:03:15.243: E/AndroidRuntime(472):  at dalvik.system.NativeStart.main(Native Method)
09-06 09:03:15.243: E/AndroidRuntime(472): Caused by: java.lang.NullPointerException
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.functions.Game.<init>(Game.java:32)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.functions.Games.addGames(Games.java:43)
09-06 09:03:15.243: E/AndroidRuntime(472):  at match.now.play.MatchNowGames.onCreate(MatchNowGames.java:52)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 09:03:15.243: E/AndroidRuntime(472):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
09-06 09:03:15.243: E/AndroidRuntime(472):  ... 11 more
09-06 09:03:15.263: I/dalvikvm(472): threadid=7: reacting to signal 3
09-06 09:03:15.273: I/dalvikvm(472): Wrote stack trace to '/data/anr/traces.txt'
09-06 09:03:17.324: I/Process(472): Sending signal. PID: 472 SIG: 9
09-06 09:03:17.873: D/dalvikvm(478): GC freed 631 objects / 50272 bytes in 64ms
09-06 09:03:18.053: D/dalvikvm(478): GC freed 65 objects / 2544 bytes in 53ms
09-06 09:03:18.183: D/dalvikvm(478): GC freed 131 objects / 6072 bytes in 57ms

Android マニフェスト:

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".functions.App" >
        <activity
            android:theme="@android:style/Theme.NoTitleBar"
            android:name=".MatchNow"
            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="MatchNowRegister" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter></activity>
        <activity android:name="MatchNowTerms" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="MatchNowMain" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="MatchNowGames" android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

アクティビティが追加されているので、Androidマニフェストは間違っていないと思います。問題はopenFileにあると思います...

別の同様のコードを使用して問題を解決しました。誰かが役に立つ場合:

public static Bitmap saveImage(String url, String fichero){
    Bitmap imageAux = null;
    OutputStream outStream = null;
    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File file = new File(extStorageDirectory, fichero);
    try {           
        // Exist
        if(file.exists()){
            imageAux=BitmapFactory.decodeFile("/sdcard/"+fichero);
        // No exist
        }else{
            outStream = new FileOutputStream(file);
            imageAux=getBITMAP.BitmapUrl(url);
            imageAux.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return imageAux;
}
4

1 に答える 1

1

openFileInput() への呼び出しのどこかに null ポインターがあります。getName() が返すもの (私の賭け) とidを確認する必要があります。

于 2012-09-06T12:08:25.563 に答える