我正在尝试创建单个图像的多个副本。在调试时,一切似乎都很好,直到我尝试越过这条线ImageView image = ...
——程序停止并给我一个Source not found错误(选项卡标题:ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1953)。
我对此感到非常沮丧......谁能告诉我为什么会这样???
主.java
package languid.legend.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* @author Adam
*
*/
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, 345);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// getting access to laytou inflater
View[] tiles = new ImageView[9];
for (int i = 0; i < tiles.length; i++) {
// Creating copy of imageview by inflating it
ImageView image = (ImageView) inflater.inflate(R.layout.singleimage, null);
tiles[i] = image;
tiles[i].setId(100+i);
params.leftMargin = 32 * 2 * i;
params.topMargin = 34 * 2 * i;
layout.addView(tiles[i]);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
单一图像.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="Sample"
android:id="@+id/image2"
android:layout_height="match_parent" />
</RelativeLayout>