新ImageView上的NullPointer

时间:2016-06-24 17:56:14

标签: java android nullpointerexception

我正在尝试开发一个简单的纸牌游戏应用,我正在尝试创建一个新的ImageView来正确放置一张卡,但是,当它达到以下方法时...

ImageView card = new ImageView(ctx);

我无处不在得到一个nullPointerException。在文件的后面,ctx在这一行中创建。

static Context ctx;

以下是围绕第366行应该发生的更详细的部分。

protected static boolean onCardPlay(String[] holding, String rankOld, RelativeLayout layoutR, int CurrentCardSlot, ImageView slot) {
    currentCard = new StringBuffer(rankOld);
    ImageView card = new ImageView(ctx); //This is line 366
    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, resources.getDisplayMetrics());
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, resources.getDisplayMetrics());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
    params.addRule(RelativeLayout.ALIGN_TOP, R.id.starting_card);
    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.starting_card);
    params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.starting_card);
    if (CardPlayLibrary.checkCardLegal(ctx, currentCard, lastCurrentCard)) {
        //place card IF it's legal
    }...

这是第187行,它位于onCreate()方法中。

card2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //returnButton2(viewCardState,playerHolding,relativelayout);
                    if (viewCardState == 0) {
                        if (Objects.equals(playerHolding[1], "empty")) {
                            Toast empty;
                            empty = Toast.makeText(ctx, "You can't play an empty card slot.", Toast.LENGTH_SHORT);
                            empty.show();
                            //CardDraw.cardEmptyError(card1);
                        } else {
                            if (onCardPlay(playerHolding, playerHolding[1], relativeLayout, 1, card2)) {
                                Log.d(TAG, ("lastCurrentCard was " + lastCurrentCard + ", and currentCard is " + Singleplayer.currentCard));
                                lastCurrentCard = currentCard;
                                currentCard = new StringBuffer(playerHolding[1]);
                                Log.d(TAG, ("lastCurrentCard is now " + lastCurrentCard + ", and currentCard is " + currentCard));
                            }
                        }
                    } else if (viewCardState == 1) {
                        if (Objects.equals(playerHolding[4], "empty")) {
                            Toast.makeText(ctx, "You can't play an empty card slot.", Toast.LENGTH_SHORT).show();
                            //CardDraw.cardEmptyError(card1);
                        } else {
                            if (onCardPlay(playerHolding, playerHolding[4], relativeLayout, 4, card2)) { //This is line 187, it contains the same code a few lines above
                                ...
                            }

还附上了一个日志

06-24 18:44:12.987 11211-11211/com.wilgo.wiltaprizes.jackchangeit
W/dalvikvm: threadid=1: thread exiting with uncaught exception(group=0x41622d88)
06-24 18:44:12.987 11211-11211/com.wilgo.wiltaprizes.jackchangeit E/AndroidRuntime: Exception
06-24 18:44:13.007 11211-11211/com.wilgo.wiltaprizes.jackchangeit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wilgo.wiltaprizes.jackchangeit, PID: 11211
java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java:352)
at android.view.View.<init>(View.java:3585)
at android.widget.ImageView.<init>(ImageView.java:114)
at com.wilgo.wiltaprizes.jackchangeit.Singleplayer.onCardPlay(Singleplayer.java:366)
at com.wilgo.wiltaprizes.jackchangeit.Singleplayer$4.onClick(Singleplayer.java:187)
at android.view.View.performClick(View.java:4575)
at android.view.View$PerformClick.run(View.java:18578)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5127)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)

我一直试图解决这个问题,但没有运气。

0 个答案:

没有答案