当我将图像添加到主视图/布局时,我遇到了运行时错误(我尝试过这两种情况)。
代码从一个活动开始,我创建游戏板没问题,当它完成游戏开始时,一切顺利。 当我按下按钮将图像添加到屏幕时出现问题,我在视图添加到主视图时出现运行时错误。 在我的代码中,我只保留所有Java,我正在避免使用XML文件,我想知道这是否可能是问题的一部分。 我一直在阅读类似问题的一些解决方案,其中人们建议使用布局inflater方法,但是如果没有XML文件,我找不到使用此方法的参考。
这是我的代码
(对不起,我在预览窗口看到它在某些时刻有多么奇怪...错误日志在最后
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);
}
LinearLayout linearL = new LinearLayout(this); linearL.setOrientation(LinearLayout.VERTICAL);
linearL.setLayoutParams(new LayoutParams(40,40));
linearL.setX(300); linearL.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):测试Barrack
08-11 03:51:39.711:V / CREATING building 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: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:线程退出未捕获异常(组= 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):at com.example.test2.GameBoard.addPicture(GameBoard.java:374)
08-11 03:51:39.761:E / AndroidRuntime(768):at 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):at 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):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11: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):at dalvik.system.NativeStart.main(Native Method)
如果有人能看到我做错了什么或者不理解,感谢很多帮助。 这是一个范围问题。 我使用的是正确类型的容器...... 我不知道:(
感谢@
答案 0 :(得分:0)
我认为问题在于您将baseBoard声明为成员变量,但在onCreate方法中使用名为baseBoard的局部变量(或者您发布的第一个代码块来自哪里)来掩盖它。
以下是我的想法:
public class MyActivity extends Activity {
RelativeLayout baseBoard;
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout baseBoard = new RelativeLayout(this);
}
}
在此代码中,您声明了两个名为“baseBoard”的不同变量。在onCreate中声明的变量会屏蔽成员变量“baseBoard”并阻止它被设置。这就是你在点击处理程序中点击空指针异常的原因。