Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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_Imageview - Fatal编程技术网

如何在android中的布局中添加多个imageview?

如何在android中的布局中添加多个imageview?,android,imageview,Android,Imageview,我正在制作一个android应用程序,其中我需要在一个布局中显示9个ImageView。但当我尝试这样做时,我得到:-'不幸的是,“AppName”已经停止了错误。 我不知道为什么会这样。我做错什么了吗?此错误仅在添加多个imageview时出现 这是我的代码 package com.yimcooldude.findtheball; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.Ac

我正在制作一个android应用程序,其中我需要在一个布局中显示9个ImageView。但当我尝试这样做时,我得到:-'
不幸的是,“AppName”已经停止了错误。
我不知道为什么会这样。我做错什么了吗?此错误仅在添加多个imageview时出现

这是我的代码

package com.yimcooldude.findtheball;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.os.Build;

public class GameActivity extends ActionBarActivity {


    ImageView ball1,ball2,ball3,ball4,ball5,ball6,ball7,ball8,ball9;
    RelativeLayout rel;
    int x, y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

overridePendingTransition(R.anim.from_middle, R.anim.to_middle);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);



        setContentView(R.layout.activity_game);


        rel=(RelativeLayout) findViewById(R.id.rel2);



        ball1 = new ImageView(this);
        ball1.setImageResource(R.drawable.ic_launcher);
        rel.addView(ball1);

        ball2 = new ImageView(this);
        ball2.setImageResource(R.drawable.ic_launcher2);
        rel.addView(ball2);

        ball3 = new ImageView(this);
        ball3.setImageResource(R.drawable.ic_launcher3);
        rel.addView(ball3);

        ball4 = new ImageView(this);
        ball4.setImageResource(R.drawable.ic_launcher4);
        rel.addView(ball4);

        ball5 = new ImageView(this);
        ball5.setImageResource(R.drawable.ic_launcher5);
        rel.addView(ball5);

        ball6 = new ImageView(this);
        ball6.setImageResource(R.drawable.ic_launcher6);
        rel.addView(ball6);

        ball7 = new ImageView(this);
        ball7.setImageResource(R.drawable.ic_launcher7);
        rel.addView(ball7);

        ball8 = new ImageView(this);
        ball8.setImageResource(R.drawable.ic_launcher8);
        rel.addView(ball9);

        ball9 = new ImageView(this);
        ball9.setImageResource(R.drawable.ic_launcher9);
        rel.addView(ball9);

        if (savedInstanceState == null) {

        }
    }

    protected void onResume()
    {
       super.onResume();       
       rel.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
       {
           @SuppressWarnings("deprecation")
        @Override
           public void onGlobalLayout()
           {
               // gets called after layout has been done but before display.
               rel.getViewTreeObserver().removeGlobalOnLayoutListener(this);


               x=(rel.getWidth()/2)-18;
               y=(rel.getHeight()/2)-18;
               TranslateAnimation animSurprise2Movement = new TranslateAnimation(0,x,0,y);
            animSurprise2Movement.setDuration(10000);
            animSurprise2Movement.setFillAfter(true);
            animSurprise2Movement.setFillEnabled(true);

           ball1.startAnimation(animSurprise2Movement);

           // get width and height
           }
       });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
活动\u game.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical"
    tools:context="com.yimcooldude.findtheball.GameActivity"
    tools:ignore="MergeRootFrame" >

    <RelativeLayout
        android:id="@+id/rel1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rel2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5" >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rel3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.5" >

    </RelativeLayout>

</LinearLayout>
编辑以下内容:

        ball8 = new ImageView(this);
        ball8.setImageResource(R.drawable.ic_launcher8);
        rel.addView(ball9);

更改以下代码:

        ball8 = new ImageView(this);
        ball8.setImageResource(R.drawable.ic_launcher8);
        rel.addView(ball9);
通过


你可以试试基本的方法

_ delete rel=(RelativeLayout) findViewById(R.id.rel2);  ( I dont know what it did ? )
_ ball1 = (ImageView) findViewby(R.id.idOfImageview)
与剩余的Imageview相同


记住在版面中创建imageview。,并在版面文件中对所有imageview进行排序。

我不知道我是如何犯下这个错误的,但你是一个救世主。
        ball8 = new ImageView(this);
        ball8.setImageResource(R.drawable.ic_launcher8);
        rel.addView(ball9);
    ball8 = new ImageView(this);
    ball8.setImageResource(R.drawable.ic_launcher8);
    rel.addView(ball8);
_ delete rel=(RelativeLayout) findViewById(R.id.rel2);  ( I dont know what it did ? )
_ ball1 = (ImageView) findViewby(R.id.idOfImageview)