从方法中加载位图时,应用程序崩溃

时间:2016-12-02 13:27:53

标签: android bitmap

我正在尝试进行多项选择测验。我想显示一个位图,以便用户猜测它是谁。因此,有一点,我需要显示一个位图。我想从一个名为setQuestionView()的函数中显示这个位图,但是当我这样做时应用程序崩溃了。

public void setQuestionView(List<String> list) {
    //Retourne d'un entier au hasard d'une liste
    intPlayer=getRandomIntPlayer(list);
    //Retourne le nom du joueur
    strPlayer=getNamePlayer(intPlayer,list);
    //Affiche le portrait du joueur en image
    strDrawable=getNameDrawable(strPlayer);
    res = getResId(strDrawable,R.drawable.class);
    bitmap = BitmapFactory.decodeResource(getResources(),res);
    img.setImageBitmap(bitmap);   
}

所以基本上,该方法获得用户必须识别的随机人(列表中的体育运动员)的名称,并且它应该显示该玩家的图像。但行

img.setImageBitmap(bitmap);

让应用崩溃。当我从方法中删除这一行然后调用方法并在方法之后写这行,它的工作原理。但我想在方法中显示图像。

你有解决方案吗?

谢谢。

以下是logcat的错误:

&#13;
&#13;
12-02 14:41:51.879 25024-25024/com.oc.rss.morphingapp E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
12-02 14:41:55.339 25024-25024/com.oc.rss.morphingapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.oc.rss.morphingapp, PID: 25024
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oc.rss.morphingapp/com.oc.rss.morphingapp.GameActivityTest}: java.lang.NullPointerException
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                            at android.os.Looper.loop(Looper.java:193)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                         Caused by: java.lang.NullPointerException
                                                                            at com.oc.rss.morphingapp.GameActivityTest.setQuestionView(GameActivityTest.java:170)
                                                                            at com.oc.rss.morphingapp.GameActivityTest.onCreate(GameActivityTest.java:77)
                                                                            at android.app.Activity.performCreate(Activity.java:5264)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                            at android.os.Looper.loop(Looper.java:193) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                            at dalvik.system.NativeStart.main(Native Method) 
&#13;
&#13;
&#13;

修改

我在我调用方法SetQuestionView

的地方添加代码
public class GameActivityTest extends AppCompatActivity {

String[] PlayersA= {"Messi","Cristiano Ronaldo"};

//"Ronaldinho","Griezman","Neymar",Pogba","Suarez","Lewandowski","Benzema",
//          "Cristiano Ronaldo","Modric","Iniesta","Hazard","Diego Costa","Gerrard","Neuer",
//        "Lloris"};
List<String> listA = Arrays.asList( PlayersA );

private int score;
Button b_good;
String strPlayer;
String strDrawable;
Bitmap bitmap;
int intPlayer;
int res;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);



    //Afficher le TopScore
    //getting preferences
    SharedPreferences prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    int TopScore = prefs.getInt("key", 0); //0 is the default value

    TextView highestScore= (TextView) findViewById(R.id.topScore);
    highestScore.setText("Top: "+ TopScore);


    ImageView img= (ImageView) findViewById(R.id.imageView);
    Button opt_1=(Button) findViewById(R.id.buttonA);
    Button opt_2=(Button) findViewById(R.id.buttonB);
    Button opt_3=(Button) findViewById(R.id.buttonC);
    Button opt_4=(Button) findViewById(R.id.buttonD);
    Button[] arr = {opt_1, opt_2, opt_3, opt_4};



    //Afficher le score
    score=getIntent().getIntExtra("score",0);
    TextView currentScore= (TextView) findViewById(R.id.currentScore);
    currentScore.setText("Score "+ score);


    setQuestionView(listA);

    //.....

1 个答案:

答案 0 :(得分:0)

问题是你在两个不同的范围内有相同的变量名。

首先你有

ImageView img;

这是一个班级字段。

但是当你分配你使用的值时

    ImageView img= (ImageView) findViewById(R.id.imageView);

这将创建一个“隐藏”类级字段的新变量。

因此,在setQuestionView方法中,类级别字段仍为空。

只需将作业行更改为

即可
    img= (ImageView) findViewById(R.id.imageView);

你应该全力以赴。