我是Android新手。我正在开发一款应用。它具有3 * 3网格布局,每个单元格中都有一个图像视图。我想从一系列drawables中随机设置图像资源。我为每一个使用了.setImageResource()但是当我运行应用程序时它会崩溃。
这是来自log cat的错误消息。
这是我的XML和Java代码。
activity_game.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.leila.makesquare.GameActivity"
android:rowCount="3"
android:columnCount="3">
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="0"
android:layout_column="0"
android:gravity="fill"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView0"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="0"
android:layout_column="1"
android:gravity="fill"
android:layout_marginTop="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="0"
android:layout_column="2"
android:gravity="fill"
android:layout_marginTop="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView2" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="0"
android:gravity="fill"
android:layout_marginLeft="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="1"
android:gravity="fill">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView4" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="2"
android:gravity="fill">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="2"
android:layout_column="0"
android:gravity="fill"
android:layout_marginLeft="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView6" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="2"
android:layout_column="1"
android:gravity="fill">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView7" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="2"
android:layout_column="2"
android:gravity="fill">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView8" />
</LinearLayout>
</GridLayout>
GameActivity.java
package com.example.leila.makesquare;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class GameActivity extends ActionBarActivity {
int[] squares=new int[]
{
R.drawable.brgy,
R.drawable.bygr,
R.drawable.bygr2,
R.drawable.gbry,
R.drawable.gyrb,
R.drawable.gyrb2,
R.drawable.rbyg,
R.drawable.rgby,
R.drawable.rybg
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
ArrayList<Integer> jj=new ArrayList<Integer>();
for (int i=0;i<9;i++) {
jj.add(i);
}
Collections.shuffle(jj);
ImageView imageView0=(ImageView)findViewById(R.id.imageView0);
ImageView imageView1=(ImageView)findViewById(R.id.imageView1);
ImageView imageView2=(ImageView)findViewById(R.id.imageView2);
ImageView imageView3=(ImageView)findViewById(R.id.imageView3);
ImageView imageView4=(ImageView)findViewById(R.id.imageView4);
ImageView imageView5=(ImageView)findViewById(R.id.imageView5);
ImageView imageView6=(ImageView)findViewById(R.id.imageView6);
ImageView imageView7=(ImageView)findViewById(R.id.imageView7);
ImageView imageView8=(ImageView)findViewById(R.id.imageView8);
int q0=squares[jj.get(0)];
int q1=squares[jj.get(1)];
int q2=squares[jj.get(2)];
int q3=squares[jj.get(3)];
int q4=squares[jj.get(4)];
int q5=squares[jj.get(5)];
int q6=squares[jj.get(6)];
int q7=squares[jj.get(7)];
int q8=squares[jj.get(8)];
imageView0.setImageResource(q0);
imageView1.setImageResource(q1);
imageView2.setImageResource(q2);
imageView3.setImageResource(q3);
imageView4.setImageResource(q4);
imageView5.setImageResource(q5);
imageView6.setImageResource(q6);
imageView7.setImageResource(q7);
imageView8.setImageResource(q8);
}
@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);
}
}
答案 0 :(得分:1)
我猜原因是你的第五个线性布局缺少关闭角括号:
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="1"
android:gravity="fill"
答案 1 :(得分:1)
Here You have not close ImageView closing bracket i.e. "/>"
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="0"
android:layout_column="0"
android:gravity="fill"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView0"
</LinearLayout>
And here you have not close Linear Layout closing bracket i.e. "/>"
<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="1"
android:gravity="fill"
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView4" />
</LinearLayout>
答案 2 :(得分:1)
您的ImageView标记未关闭。尝试
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView0"/>
尝试给我们提供日志
[编辑] 从编辑开始,您的活动代码非常糟糕,请尝试阅读有关GridViews的更多信息以及Java和Android中的编码实践,以提高您的代码和编码技能。