Android中空指针异常的可绘制数组错误

时间:2015-09-16 11:43:02

标签: android drawable

我正在对使用可绘制数组的活动执行某项任务,这会引发错误,请仔细检查。任何帮助都将受到高度赞赏。

package com.example.waleedmalik.computerparts;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class GameActivity extends AppCompatActivity {
ImageView iv;
Button btnNext;
RadioGroup rgp;
RadioButton rb1, rb2, rb3, rb4;

int [] questions = {

        R.drawable.cd,
        R.drawable.cdrom,
        R.drawable.cpu,
        R.drawable.flashusb,
        R.drawable.floppy,
        R.drawable.harddisk,
        R.drawable.mic,
        R.drawable.monitor,
        R.drawable.mouse,
        R.drawable.printer,
        R.drawable.ram,
        R.drawable.speakers,
        R.drawable.usb,
        R.drawable.dvdrom,
        R.drawable.dvd,
        R.drawable.motherboard

};

String ans[] = {
        "CD",
        "CD-ROM",
        "CPU",
        "CARD-READER",
        "FLOPPY DISK",
        "HARD-DISK",
        "MICROPHONE",
        "MONITOR",
        "MOUSE",
        "PRINTER",
        "RAM",
        "SPEAKERS",
        "USB",
        "DVD-ROM",
        "DVD",
        "MOTHER-BOARD"
};

String opt[] = {"RAM", "DISK", "CD", "FLASH-PLAYER", "HARD-DISK", "DVD-ROM", "CD-ROM", "FLOPPY DISK", "FLASH-USB", "CD-DRIVE", "PRINTER", "CPU","USB", "CARD-READER", "PEND-DRIVE", "LASER",
        "HARD-DISK", "FLOPPY-DISK", "RAM", "JACKET", "DVD-ROM", "MOTHER-BOARD", "HARD-DISK", "SMART-PHONE", "SPEAKER", "RECEIVER", "MICROPHONE", "HEAD-PHONE", "TELEVISION", "MONITOR", "SCREEN", "DISPLAY",
        "MICE", "MOUSE", "HOLDER", "SLIDER", "FAX", "PRINTER", "DESKTOP", "MICROWAVE-OVEN", "ROM", "RAM", "CD-ROM", "HARD-DISK", "MICROPHONE", "SPEAKERS", "BASS-SYSTEM", "ANTENNA", "PORTABLE", "PLUGIN", "USB", "HARD-DRIVE",
        "DVD-ROM", "HARD-DISK", "MOUSE", "FLOPPY-DISK", "CD", "DVD", "MAGNETIC-TAPE", "SPINNER", "MOTHER-BOARD", "CIRCUIT", "KEYBOARD", "DASHBOARD"};


int flag = 0;
public static int marks, correct, wrong;

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

    iv = (ImageView) findViewById(R.id.imageView);
    btnNext = (Button) findViewById(R.id.next);
    rgp = (RadioGroup) findViewById(R.id.rg);
    rb1 = (RadioButton) findViewById(R.id.rb1);
    rb2 = (RadioButton) findViewById(R.id.rb2);
    rb3 = (RadioButton) findViewById(R.id.rb3);
    rb4 = (RadioButton) findViewById(R.id.rb4);

    iv.setImageResource(questions[flag]);
    rb1.setText(opt[0]);
    rb2.setText(opt[1]);
    rb3.setText(opt[2]);
    rb4.setText(opt[3]);


    Toast.makeText(this, "Negative Marking: " + SecondActivity.tbflag, 
Toast.LENGTH_LONG).show();


    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            RadioButton uans = (RadioButton) 
findViewById(rgp.getCheckedRadioButtonId());
            String anstxt = uans.getText().toString();

            if (anstxt.equalsIgnoreCase(ans[flag])) {

                correct++;

            } else {

                wrong++;
            }

            flag++;
            if (flag < questions.length) {

                iv.setImageResource(questions[flag]);
                rb1.setText(opt[flag * 4]);
                rb2.setText(opt[(flag * 4) + 1]);
                rb3.setText(opt[(flag * 4) + 2]);
                rb4.setText(opt[(flag * 4) + 3]);
            }
            else
            {

                if (SecondActivity.tbflag) {

                    marks = correct - wrong;
                }
                else
                {
                    marks = correct;
                }
                Intent in = new Intent(getApplicationContext(), 
ResultActivity.class);
                startActivity(in);
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

logcat的

java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 3760512 free bytes and 3MB until OOM
            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
            at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
            at android.content.res.Resources.loadDrawable(Resources.java:2540)
            at android.content.res.Resources.getDrawable(Resources.java:806)
            at android.content.Context.getDrawable(Context.java:458)
            at android.widget.ImageView.resolveUri(ImageView.java:811)
            at android.widget.ImageView.setImageResource(ImageView.java:418)
            at com.example.waleedmalik.computerparts.GameActivity.onCreate(GameActivity.java:88)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:1)

你制作一个这样的可绘制数组:

 int[] draw = {R.drawable.d0
                    R.drawable.d1
                    R.drawable.d2
                    R.drawable.d3
                    R.drawable.d4
            };

本质上它是int数组而不是Drawable数组。

在您的情况下,将声明Drawable[] drawables = null;更改为int[] drawables = null;