0

将图像添加到主视图/布局时出现运行时错误(我尝试了两者)。

代码从一个活动开始,我创建游戏板没问题,完成后游戏开始,一切顺利。当我按下按钮将图像添加到屏幕时会出现此问题,在将视图添加到主视图时出现运行时错误。在我的代码中,我只保留所有 Java,我避免使用 XML 文件,我想知道这是否可能是问题的一部分。我一直在阅读一些类似问题的解决方案,人们建议使用布局充气方法,但是如果没有 XML 文件,我找不到使用这种方法的参考。

这是我的代码

(PS 抱歉,我在预览窗口中看到它在某些时候中断了多么奇怪......错误日志在最后

     RelativeLayout GameView = new RelativeLayout(this);
     GameView.setLayoutParams(new LayoutParams(
             screenHei,
             screenLen));




     //Start Game sequence
     ZoomView layout3 = new ZoomView(this);


     layout3.setLayoutParams(new LayoutParams(
             screenHei,
             screenLen));


     //Relative layout... aka base view that will be inserted in the zoom view
     RelativeLayout baseBoard = new RelativeLayout(this);
     //LinearLayOut Setup


     baseBoard.setLayoutParams(new LayoutParams(
             screenHei,
             screenLen));


        LinearLayout linearLayout= new LinearLayout(this);

        linearLayout.setOrientation(LinearLayout.VERTICAL);

        linearLayout.setLayoutParams(new LayoutParams(
                 screenHei,
                 screenLen));
        //ImageView Setup
        ImageView imageView = new ImageView(this);
        //setting image resource
        imageView.setImageResource(R.drawable.bluepage);
        //setting image position

        imageView.setLayoutParams(new LayoutParams(
                 screenHei,
                 screenLen));
        //adding view to layout
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        linearLayout.addView(imageView);
        //make visible to program
        linearLayout.setX(0);
        linearLayout.setY(0);
//   RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
//              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
//      lp.addRule(RelativeLayout.BELOW, v.getId());

        baseBoard.addView(linearLayout); 
     layout3.setX(0);
     layout3.setY(0);



        final RelativeLayout menuMobile = new RelativeLayout(this);


         menuMobile.setLayoutParams(new LayoutParams(
                 200,
                 200));
         menuMobile.setBackgroundColor(0xFF000000 );

         Button ButtonMobile1 = new Button(this);
         ButtonMobile1.setBackgroundColor(0xFFCCFFCC);
         ButtonMobile1.setText("test1");
         ButtonMobile1.setTextSize(6);
         ButtonMobile1.setLayoutParams(new LayoutParams(155,66));
         ButtonMobile1.setX(100);
         ButtonMobile1.setY(40);
         menuMobile.addView(ButtonMobile1); 
         ButtonMobile1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                 Log.v("prepare to add barrack! "," testing Barrack ");
                 menuMobile.setVisibility(View.INVISIBLE);

                 @SuppressWarnings("rawtypes")
                ArrayList aList = (ArrayList) GroundWork.get(selectedX);
                 GameTile axs = (GameTile) aList.get(selectedY);

                   axs.selected=1;
                   axs.createBuilding(1,1,1);


                   addPicture(axs.imgSizeIds);


                   Log.v("tile selected value and information "," axs.selected :" + axs.selected);
                }
            });


     valueX=21;
     valueY=12;
     //Base map builder
     for(int l=0; l<=valueX; l++){
        dimension.clear() ;
        for (int w=0; w<=valueY; w++){


    // ArrayList<Button> dimension = new ArrayList<Button>();

     LinearLayout buttonLayout= new LinearLayout(this);
     buttonLayout.setOrientation(LinearLayout.VERTICAL);
     buttonLayout.setLayoutParams(new LayoutParams(
                40,
                40));

     buttonLayout.setX(l*40);
     buttonLayout.setY(w*40);   

     System.out.println("Object Creation");
     final GameTile Tile = new GameTile();
     Tile.onCreate();
     Tile.pos_x=l;
     Tile.pos_y=w;
     System.out.println("Object                       !! CREATED !!");

     System.out.println("Object  added to prelim layout");

     Button b1 = new Button(this);
     b1.setBackgroundColor(0xFFCCFFCC);
     b1.setText(" ");
     b1.setLayoutParams(new LayoutParams(38,38));
     buttonLayout.addView(b1);
     baseBoard.addView(buttonLayout); 


        b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                 Log.v(Tile.button_hit,Tile.button_hit);
                // toggleMobileMenu();
                 menuMobile.setVisibility(View.VISIBLE);

                 menuMobile.setX(Tile.pos_x*40);
                 menuMobile.setY(Tile.pos_y*40);

                 selectedX=Tile.pos_x;               
                 selectedY=Tile.pos_y;
                }
            });


        dimension.add(Tile); 


        }
        GroundWork.add(dimension); 
        System.out.println(" "+l);
     }

线性布局线性L=新线性布局(这个);线性L.setOrientation(LinearLayout.VERTICAL);

线性L.setLayoutParams(new LayoutParams(40, 40));

线性L.setX(300); 线性L.setY(340);

ImageView iv = new ImageView(this);

iv.setImageResource(R.drawable.castle);

       iv.setX(selectedX*40);
       iv.setY(selectedY*40);

        linearL.addView(iv); 

        player1img.add(iv); 

            linearL.setLayoutParams(new ViewGroup.LayoutParams(40,40));

        baseBoard.addView(linearL);

//   setContentView(layout);
     Log.v("Added view to program", "Added view to program");

     imageView.setBackgroundColor(0xFF000000 );
     linearLayout.setBackgroundColor(0xFF000000 );
    layout3.setBackgroundColor(0xFF000000 );

    int ACount;
    ACount = GroundWork.size();
    System.out.println("ACount");
    System.out.println(ACount);

    //Casting test
     //ArrayList<Button> checking = GroundWork.get(2);
    //Temp solution

        int BCount;
        BCount = dimension.size(); 
        System.out.println("BCount");
        System.out.println(BCount);

    //Adding final product to view  
         RelativeLayout menuView = new RelativeLayout(this);


         menuView.setLayoutParams(new LayoutParams(
                 screenHei,
                 150));
         menuView.setBackgroundColor(0xFF000000 );

         Button sendUnit = new Button(this);
         sendUnit.setBackgroundColor(0xFFCCFFCC);
         sendUnit.setText("test");
         sendUnit.setTextSize(8);
         sendUnit.setLayoutParams(new LayoutParams(155,66));
         sendUnit.setX(100);
         sendUnit.setY(40);
         menuView.addView(sendUnit); 
         sendUnit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                 Log.v(" test "," test ");
                 menuMobile.setVisibility(View.INVISIBLE);

                }
            });

        layout3.addView(baseBoard); 
        GameView.addView(layout3); 
        GameView.addView(menuView); 

        //Activating final View
        setContentView(GameView);


        GameView.addView(menuMobile); 


}

public void addPicture (int imgSizeIds) {



     LinearLayout linearL= new LinearLayout(this);
     linearL.setOrientation(LinearLayout.VERTICAL);
     linearL.setLayoutParams(new LayoutParams(
                 40,
                 40));

     linearL.setX(selectedX*40);
     linearL.setY(selectedY*40);

    Log.v(" Adding Image in view and array ","   " + imgSizeIds);
    ImageView iv = new ImageView(this);

     Log.v(" Setting Image ","   " + imgSizeIds);
     iv.setImageResource(imgSizeIds);   
     iv.setImageResource(R.drawable.castle);
       Log.v("  Image assigned","   " + imgSizeIds);   
       iv.setX(selectedX*40);
       iv.setY(selectedY*40);

     Log.v(" Adding Image in layout  ","   " + imgSizeIds);
    linearL.addView(iv); 

      //  LayoutInflater popupLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        Log.v(" Adding layout in   Mainview ","   " + imgSizeIds);
        //linearL.showAtLocation(baseBoard, android.view.Gravity.NO_GRAVITY, 100, 100);
        //linearL = popupLayoutInflater.inflate(R.layout.baseBoard, null);

        linearL.setLayoutParams(new ViewGroup.LayoutParams(40,40));


  //     RelativeLayout lL = findViewById(baseBoard);

   //           lL.addView(linearL);




        baseBoard.addView(linearL); 

         Log.v(" Adding in       array ","   " + imgSizeIds);
           player1img.add(iv); 

    Log.v(" COMPLETED  ","   " + imgSizeIds);
}

}

错误日志

08-11 03:51:38.521: V/this_is_a_fucking_joke(768): this_is_a_fucking_joke 08-11 03:51:39.701: V/准备添加兵营!(768): 测试兵营 08-11 03:51:39.711: V/正在创建建筑物 1(768):正在创建建筑物 1
08-11 03:51:39.711:V/正在创建对象的建筑物末端(768):正在创建对象的建筑物末端
08-11 03:51:39.711: V/在视图和数组中添加图像 (768): 2130837505 08-11 03:51:39.711: V/设置图像 (768): 2130837505 08-11 03:51:39.711: V /图像分配(768):2130837505 08-11 03:51:39.721:V /在布局中添加图像(768):2130837505 08-11 03:51:39.721:V /在Mainview中添加布局(768):2130837505 08- 11 03:51:39.721: D/AndroidRuntime(768): 关闭 VM 08-11 03:51:39.721: W/dalvikvm(768): threadid=1: 线程退出但未捕获异常 (group=0x40a71930) 08-11 03:51:39.761:E/AndroidRuntime(768):致命异常:主要 08-11 03:51:39.761:E/AndroidRuntime(768):java.lang.NullPointerException 08-11 03:51:39.761:E/AndroidRuntime (768): 在 com.example.test2.GameBoard.addPicture(GameBoard.java:374) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 com.example.test2.GameBoard$1.onClick(GameBoard .java:169) 08-11 03:51:39.761:E/AndroidRuntime(768): 在 android.view.View.performClick(View.java:4204) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 android.view.View$PerformClick.run(View .java:17355) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 android.os.Handler.handleCallback(Handler.java:725) 08-11 03:51:39.761: E/AndroidRuntime(768) ): 在 android.os.Handler.dispatchMessage(Handler.java:92) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 android.os.Looper.loop(Looper.java:137) 08- 11 03:51:39.761: E/AndroidRuntime(768): 在 android.app.ActivityThread.main(ActivityThread.java:5041) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 java.lang。 reflect.Method.invokeNative(Native Method) 08-11 03:51:39.761: E/AndroidRuntime(768): at java.lang.reflect.Method.invoke(Method.java:511) 08-11 03:51:39.761 : E/AndroidRuntime(768): 在 com.android.internal.os。ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-11 03:51:39.761: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-11 03:51:39.761: E/AndroidRuntime(768): 在 dalvik.system.NativeStart.main(Native Method)

非常感谢您的帮助,如果有人能看到我做错了什么或不理解。这是一个范围问题。我是否使用了正确类型的容器...我不知道 :(

谢谢@

4

1 回答 1

0

我认为问题在于您将 baseBoard 声明为成员变量,但在 onCreate 方法(或您发布的第一个代码块的来源)中使用名为 baseBoard 的局部变量将其屏蔽。

这是我认为正在发生的事情:

public class MyActivity extends Activity {
    RelativeLayout baseBoard;

    protected void onCreate(Bundle savedInstanceState) {
        RelativeLayout baseBoard = new RelativeLayout(this);
    }
}

在此代码中,您声明了两个不同的变量,称为“baseBoard”。在 onCreate 中声明的变量掩盖了成员变量“baseBoard”并防止它被设置。这就是您在单击处理程序中遇到空指针异常的原因。

于 2013-08-11T04:19:59.380 回答