Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中设置ImageView资源时出错_Android_Android Imageview_Android Gridlayout - Fatal编程技术网

在android中设置ImageView资源时出错

在android中设置ImageView资源时出错,android,android-imageview,android-gridlayout,Android,Android Imageview,Android Gridlayout,我是Android新手。我正在开发一个应用程序。它有一个3*3的网格布局,每个单元格中都有一个图像视图。我想从一个可绘制的数组中随机设置图像资源。我对每个应用程序都使用了.setImageResource(),但当我运行应用程序时,它崩溃了 下面是来自的错误消息 这是我的XML和java代码 活动\u game.xml <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools

我是Android新手。我正在开发一个应用程序。它有一个3*3的网格布局,每个单元格中都有一个图像视图。我想从一个可绘制的数组中随机设置图像资源。我对每个应用程序都使用了.setImageResource(),但当我运行应用程序时,它崩溃了

下面是来自的错误消息

这是我的XML和java代码

活动\u 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);
    }
}
package com.example.leila.makesquare;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.ImageView;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
公共类GameActivity扩展了ActionBarActivity{
整数[]平方=新整数[]
{ 
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
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_游戏);
ArrayList jj=新的ArrayList();

对于(int i=0;i您在第五个线性布局中忘记了一个闭合角括号:

<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="1"
android:gravity="fill"
这里您没有关闭ImageView关闭括号,即“/>”

您的ImageView标记未关闭。请重试

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView0"/>

试着把日志给我们

[编辑]
由于编辑你的活动代码非常糟糕,请尝试阅读更多关于GridView以及Java和Android中的编码实践的信息,以提高你的代码和编码技能。

从你的日志中添加崩溃日志这是一个非常普通的代码。如果你在谷歌中搜索GridLayout示例,这可能会更好,而且这段代码很奇怪。如果你需要,你不需要jj数组你甚至不需要q0-q8,因为你可以直接在imageView0中使用正方形[0]。setImageResource(正方形[0])。另外,正如@Piyush提到的,直到您的要求是非常非标准的网格,这不是实现网格布局的正确方法使用图像加载库加载图像,您的应用程序崩溃只是因为您的设备无法分配足够的内存来加载网格视图特定单元格中的高分辨率图像,所以请使用Fresco,Glide、Picaso或Universal Image loader Library这可能不是问题。我不明白,所有线性布局和图像视图的开/关括号都是相同的。这不是答案!这不是答案!
    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView0"/>