【问题标题】:Gridview activity cannot be call from another activity不能从另一个活动调用 Gridview 活动
【发布时间】:2015-10-10 21:03:56
【问题描述】:

这是我之前的问题How to call the Gridview Activity from another activity 的更新,我添加了我的代码

这是我的画廊.xml

 <?xml version="1.0" encoding="utf-8"?>
 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

图库.java

 package com.example.diaz.chmscadvertisement;


 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.widget.GridView;

 public class Gallery extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
 }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
}

ImageAdapter.java

 package com.example.diaz.chmscadvertisement;

 import android.content.Context;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.GridView;
 import android.widget.ImageView;

 /**
  * Created by diaz on 10/10/2015.
  */
      public class ImageAdapter extends BaseAdapter {
     private Context mContext;

     public ImageAdapter(Context c) {
         mContext = c;
     }

     public int getCount() {
         return mThumbIds.length;
     }

     public Object getItem(int position) {
         return null;
     }

     public long getItemId(int position) {
         return 0;
     }

     // create a new ImageView for each item referenced by the Adapter
     public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample2, R.drawable.sample3,
        R.drawable.sample4, R.drawable.sample5,
        R.drawable.sample6, R.drawable.sample7,
        R.drawable.sample10, R.drawable.sample1,
        R.drawable.sample8, R.drawable.sample9,

};
 }

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.diaz.chmscadvertisement" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/logoalijis"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity android:name=".ViewFlipperBody" />
        <activity android:name=".AboutChmsc" />
        <activity android:name=".CampusLife" />
        <activity android:name=".AboutUs" />
        <activity android:name=".MainMenu"></activity>
        <activity android:name=".Gallery"/>
        <activity
            android:name=".SplashScreen"
            android:label="CHMSC-Alijis" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的主菜单这是我按下按钮以打开 Gridview 的地方

MainMenu.java

package com.example.diaz.chmscadvertisement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class MainMenu extends Activity implements View.OnClickListener {

Button button2;
Button button3;
Button button4;
Button button6;
Button button7;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);

        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);

    }

    public void onClick(View v){
        if (v.getId() == R.id.button2) {
            Intent intent = new Intent(this, ViewFlipperBody.class);
            this.startActivity(intent);

        } else if (v.getId() == R.id.button3) {
            Intent intent = new Intent(this, AboutChmsc.class);
            this.startActivity(intent);

        } else if (v.getId() == R.id.button4) {
            Intent intent = new Intent(this, CampusLife.class);
            this.startActivity(intent);
        }

        else if (v.getId() == R.id.button6) {
            Intent intent = new Intent(this, Gallery.class);
            this.startActivity(intent);
        }

        else if (v.getId() == R.id.button7) {
            Intent intent = new Intent(this, AboutUs.class);
            this.startActivity(intent);
        }

    }
}

问题是当我单击 Gallery 按钮(即 MainMenu.java 中的 button6)时,gallery.xml 根本没有打开。如果我用 R.drawable.icon_launcher 替换 mThumbIds 的元素,它将正常工作。知道吗?提前致谢

【问题讨论】:

    标签: android android-gridview


    【解决方案1】:

    您应该在gallery.xml中添加一个布局来包裹GridView,否则它的宽度和高度将是0dp。

    【讨论】:

    • 你的意思是 ?
    • 是的,给gridview添加一个父布局,如果添加xml作为activity的布局,可以添加merge进行优化。
    • 你能给我举个例子吗?我是android新手。
    • 请看我的另一个答案。
    【解决方案2】:

    如果图像不可用,您的 Gridview 将不会显示。请使用 ic_launcher 代替示例图像尝试相同的代码。

    【讨论】:

    • 它不起作用,因为如果我按下按钮6,它将引导我进入画廊活动,除了我那些示例图像已经在 res->.drawable 目录中之外,什么也没有发生。但如果这就是你所能做的,我想我稍后会尝试你的建议。谢谢
    • 您的建议确实有效。它打开了我的画廊活动。您知道这些示例图像是如何解决的吗?我很确定它们在可绘制对象内
    • 如果你还卡在这里..请给我你的示例图片
    • 你指的是我的实际图片吗?抱歉,我不知道如何在 stockoverflow 中发布图片。
    【解决方案3】:
    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <GridView 
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
    </merge>
    

    【讨论】:

    • 您的建议无效。正如 Sukvhir 在上面所建议的那样,它确实已经显示了 ic_launcher,这意味着问题一定是我的形象。可能是分辨率大小?
    • 根据 Sukvhir 的建议,我遇到了一个新问题,我得出的结论是,这与我调用的图像的分辨率有关,因为它们的分辨率大小大多在 700x500 之间,所以我决定改变使用任何图像编辑器(如 photoshop)将它们的大小调整为 50x50 左右,但大小似乎与绘制 ic_launcher 时的大小相同,所以这次我再次将其更改为 250x250 左右,但它再次显示与 ic_launcher 相同的大小。你知道这是怎么发生的吗?
    • 我认为那是因为你的项目视图只是一个图像视图,没有父布局包裹它,所以它的大小不起作用。
    • 你指的是我的gallery.xml吗?你介意给我举个例子吗?我现在没有时间将其包含在我的项目中,因为我正在寻找另一种方法,但如果问题得到解决,我很乐意从中学到一些东西
    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多