我有一個類,我嘗試在Android應用程序中使用AssetManager讀取圖像。我必須在另一個類中調用此類。本地變量可能尚未初始化爲AssetManager可繪製
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
public class AssetActivity extends Activity {
private static final String TAG = "AssetActivity";
public Drawable getImage(String imgName) {
AssetManager assets = getAssets(); // get app's AssetManager
InputStream stream; // used to read in Image images
String nextImageName = imgName;
Drawable flag;
try {
// get an InputStream to the asset representing the next Image
stream = assets.open(nextImageName + ".jpg");
// load the asset as a Drawable and display on the objImageView
flag = Drawable.createFromStream(stream, nextImageName);
} // end try
catch (IOException e) {
Log.e(TAG, "Error loading " + nextImageName, e);
} // end catch
return flag;
}}
我得到錯誤局部變量標誌可能尚未初始化。 請告訴我如何避免這個錯誤。提前致謝。