1

在一个大项目中,我有以下运行步骤的代码
首先是 GalleryMeno 运行,然后是 GalleryActivityMain

GalleryMeno.java

public class GalleryMeno  extends Activity{

private Integer[] mThumbIds = {
        R.drawable.gallary_farhad1 ,
        R.drawable.gallary_farhad2,
        R.drawable.gallary_naser ,
        R.drawable.gallary_mehdi ,
        R.drawable.gallary_other ,
        R.drawable.gallary_old ,
        R.drawable.gallary_amir ,
        R.drawable.gallary_bazikonan
};

private String[] name = {
        "gallary_farhad1",
        "gallary_farhad2",
        "gallary_naser",
        "gallary_mehdi",
        "gallary_other",
        "gallary_old",
        "gallary_amir",
        "gallary_bazikonan"
};

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_meno_activity);

    TextView titleName = (TextView) findViewById(R.id.title_name);
    titleName.setText("گالری تصاویر");


    //found width of Screen for Gridview
    WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    int densityX = display.getWidth();  

    Button btnUpdate = (Button) findViewById(R.id.update);
    btnUpdate.setVisibility(View.INVISIBLE);

    Button backButton = (Button) findViewById(R.id.backward);
    backButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-g

            finish();
        }
    });

    GridView grid = (GridView) findViewById(R.id.gridViewSms);
    grid.setAdapter(new ImageAdapter(getApplicationContext(), densityX, mThumbIds));

    grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position ,
                long id) {

            Intent MainGallery = new Intent(GalleryMeno.this , GalleryActivityMain.class);
            MainGallery.putExtra("position", position);
            MainGallery.putExtra("title", name[position]);

            startActivity(MainGallery);     
        }
    });
}
}  

ImageAdapter .java

public class ImageAdapter extends BaseAdapter {
 private Context mContext;
 private int widthOfScrren;
 public Integer[] mThumbIds;

    public ImageAdapter(Context c,int ScrrenSize,Integer[] mThumbIds){
        mContext = c;
        widthOfScrren=ScrrenSize;
        this.mThumbIds = mThumbIds;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        final int w;
        if ( widthOfScrren == 0 )
            w =  75;
        else
            w = widthOfScrren/3;
        imageView.setLayoutParams(new GridView.LayoutParams(w,w));
        return imageView;
    }   
}

GalleryActivityMain.java

public class GalleryActivityMain extends Activity {
   public static Integer[] ImageGroup1 = {
       R.drawable.farhad01,
       R.drawable.farhad02,
       R.drawable.farhad03,
       R.drawable.farhad04,
       R.drawable.farhad05,
       R.drawable.farhad06,
       R.drawable.farhad07,
       R.drawable.farhad08,
       R.drawable.farhad09,
       R.drawable.farhad10,
       R.drawable.farhad11,
       R.drawable.farhad12,
       R.drawable.farhad13,
       R.drawable.farhad14,
       R.drawable.farhad15,
       R.drawable.farhad16

    };
   public static Integer[] ImageGroup2 = {
       R.drawable.farhad17,
       R.drawable.farhad18,
       R.drawable.farhad19,
       R.drawable.farhad20,
       R.drawable.farhad21,
       R.drawable.farhad22,
       R.drawable.farhad23,
       R.drawable.farhad24,
       R.drawable.farhad25,
       R.drawable.farhad26,
       R.drawable.farhad27,
       R.drawable.farhad28,
       R.drawable.farhad29,
       R.drawable.farhad30,
       R.drawable.farhad31,
       R.drawable.farhad32,
       R.drawable.farhad33

   };   

   public static Integer[] ImageGroup3 = {  
       R.drawable.naser01,
       R.drawable.naser02,
       R.drawable.naser03,
       R.drawable.naser04,
       R.drawable.naser05,
       R.drawable.naser06,
       R.drawable.naser07,
       R.drawable.naser08,
       R.drawable.naser09,
       R.drawable.naser10,
       R.drawable.naser11,
       R.drawable.naser12,
       R.drawable.naser13,
       R.drawable.naser14,
       R.drawable.naser15,
       R.drawable.naser16,
       R.drawable.naser17
   };

   public static Integer[] ImageGroup4 = {
       R.drawable.mehdi01,
       R.drawable.mehdi02,
       R.drawable.mehdi03,
       R.drawable.mehdi04,
       R.drawable.mehdi05,
       R.drawable.mehdi06,
       R.drawable.mehdi07,
       R.drawable.mehdi08,
       R.drawable.mehdi09,
       R.drawable.mehdi10,
       R.drawable.mehdi11,
       R.drawable.mehdi12,
       R.drawable.mehdi13,
       R.drawable.mehdi14,
       R.drawable.mehdi15,
       R.drawable.mehdi16,
       R.drawable.mehdi17
   };

   public static Integer[] ImageGroup5 = {
       R.drawable.other01,
       R.drawable.other02,
       R.drawable.other03,
       R.drawable.other04,
       R.drawable.other05,
       R.drawable.other06,
       R.drawable.other07,
       R.drawable.other08,
       R.drawable.other09,
       R.drawable.other10,
       R.drawable.other11,
       R.drawable.other12,
       R.drawable.other13,
       R.drawable.other14,
       R.drawable.other15,
       R.drawable.other16,
   };

 public static Integer[] ImageGroup6 = {

       R.drawable.old01,
       R.drawable.old02,
       R.drawable.old03,
       R.drawable.old04,
       R.drawable.old05,
       R.drawable.old06,
       R.drawable.old07,
       R.drawable.old08,
       R.drawable.old09,
       R.drawable.old10,
       R.drawable.old11,
       R.drawable.old12,
       R.drawable.old13,
       R.drawable.old14,
       R.drawable.old15,
       R.drawable.old16,
       R.drawable.old17,
       R.drawable.old18,
       R.drawable.old19

   };

   public static Integer[] ImageGroup7 = {

       R.drawable.amir01,
       R.drawable.amir02,
       R.drawable.amir03,
       R.drawable.amir04,
       R.drawable.amir05,
       R.drawable.amir06,
       R.drawable.amir07,
       R.drawable.amir08,
       R.drawable.amir09,
       R.drawable.amir10,
       R.drawable.amir11,
       R.drawable.amir12,
       R.drawable.amir13,
       R.drawable.amir14,
       R.drawable.amir15
   };

   public static Integer[] ImageGroup8 = {
        R.drawable.b1,
        R.drawable.b2,
        R.drawable.b3,
        R.drawable.b4,
        R.drawable.b5,
        R.drawable.b6,
        R.drawable.b7,
        R.drawable.b8,
        R.drawable.b9,
        R.drawable.b10,
        R.drawable.b11,
        R.drawable.b12,
        R.drawable.b13,
        R.drawable.b14,
        R.drawable.b15,
        R.drawable.b16,
        R.drawable.b17,
        R.drawable.b18,
        R.drawable.b19,
        R.drawable.b20
   };

   public static Integer[] SelectedGroup;

private int Global_position;

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

    TextView titleName = (TextView) findViewById(R.id.title_name);

    Intent current = getIntent();
    Global_position=current.getExtras().getInt("position");

    titleName.setText(current.getExtras().getString("title"));      

    Button btnUpdate = (Button) findViewById(R.id.update);
    btnUpdate.setVisibility(View.INVISIBLE);

    Button backButton = (Button) findViewById(R.id.backward);
    backButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();

        }
    });

    GridView grid=(GridView) findViewById(R.id.grid_view_gallery);

    //connect xml_gridView to Class ImageAdapter
    //when we sent 0 to screensize we want to auto generating image independent device
    grid.setAdapter(new ImageAdapter(getApplicationContext(), 0, getCorrectItem(Global_position)));     

    //set item on click listener
    grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {

            Intent FullImage = new Intent(getApplicationContext(), GalleryFullImageView.class);
            /******
             * by below syntax we sent some information by a id 
             * to new intent and on new intent we extract it and use it
            ******/
            FullImage.putExtra("position", position);
            startActivity(FullImage);
        }
    });
}

private Integer[] getCorrectItem(int position)
{
    switch (position) {

    case 0:
        SelectedGroup = ImageGroup1;
        return  ImageGroup1;

    case 1:
        SelectedGroup = ImageGroup2;
        return  ImageGroup2;

    case 2:
        SelectedGroup = ImageGroup3;
        return  ImageGroup3;

    case 3:
        SelectedGroup = ImageGroup4;
        return  ImageGroup4;

    case 4:
        SelectedGroup = ImageGroup5;
        return  ImageGroup5;

    case 5:
        SelectedGroup = ImageGroup6;
        return  ImageGroup6;    

    case 6:
        SelectedGroup = ImageGroup7;
        return  ImageGroup7;    
    case 7:
        SelectedGroup = ImageGroup8;
        return  ImageGroup8;            
    default:
        SelectedGroup = ImageGroup7;
        return ImageGroup7;
    }   
}
}

Mymanifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.esteghlaliha"
android:versionCode="8"
android:installLocation="preferExternal"
android:versionName="1.9" >

<uses-sdk
    android:minSdkVersion="6"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/esteghlal_big"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.perspolisiha.Esteghlal"
        android:theme="@android:style/Theme.NoTitleBar"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name="com.example.perspolisiha.Main"
         android:configChanges="orientation|screenSize"
        android:theme="@android:style/Theme.NoTitleBar">
    </activity>

    <activity android:name="com.example.rank.Resultshow" 
         android:configChanges="orientation|screenSize"
         android:screenOrientation="landscape"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.rank.RankLayout"  
         android:configChanges="orientation|screenSize"
        android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.perspolisiha.SmsshowActivity"  
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.perspolisiha.GalleryActivityMain"  
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.perspolisiha.GalleryFullImageView" 
         android:configChanges="orientation|screenSize" 
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.perspolisiha.GalleryMeno"
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.tv.TVLayout"  
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.tv.TVResultshow" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.bio.BioMainActivity" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

    <activity android:name="com.example.bio.BioResultShow" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

     <activity android:name="com.example.history.HistoryActivity" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

     <activity android:name="com.example.news.NewsActivityMain" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

     <activity android:name="com.example.news.ShowDetailActivity" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

     <activity android:name="com.example.news.setting" 
         android:configChanges="orientation|screenSize"
         android:theme="@android:style/Theme.NoTitleBar"/>

</application>


</manifest>

Case1:当我在 android 2.2 中运行上面的代码时,我看到了一个由 gridview 制作的图像菜单,
当我点击每个项目时,所有关于该项目的图像都显示出来。(在这种情况下,所有结果都可以)


Case2:但是当我通过单击项目(查看有关它的所有图像) 在 android 4.2 中运行相同的代码时,我的程序在没有任何强制关闭的情况下退出

例如:当我单击图像时,gallary_amir我希望看到来自ImageGroup7
Why的图像网格视图?如何修复 Case2?

4

1 回答 1

1

您说您没有收到任何强制关闭,因此没有 LogCat。我要说的是,既然您正在检查Android 4.2哪个是API level 17,请尝试使用您的android:targetSdkVersion=""as17而不是,16看看您是否收到任何错误LogCat。如果您阅读有关android:targetSdkVersion的信息,它会说:

If the API level of the platform is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your app continues to work the way you expect.

当您在目标的更高版本上进行测试时,Android 会尝试启用这些兼容性行为。可能有一些函数调用或其他一些东西在 API 版本 17 中发生了变化,并且行为正常。另一点是尝试在您的代码中查看哪些东西已针对 API 17 折旧并已在您的代码中使用。始终建议使用最新的东西。

希望这在某种程度上有所帮助。

于 2013-07-31T03:15:38.313 回答