1

私はレンガの壁にぶつかりました.私が従ったチュートリアルは、クリックすると画像を拡大するだけです.

私がやりたいのは、画像ごとに異なるアクティビティを開くことです。

たとえば、代数画像は代数活動を開き、小数画像をクリックすると小数活動が開きます。

これはおそらく単純ですが、現時点ではあまり明白ではありません。

主な活動はこちら。

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView;  
public class MathsSubjectsActivity extends Activity {

    // Keep all Images in array
    public Integer[] mThumbIds = {
        R.drawable.algebraicon,R.drawable.coordsicon,
        R.drawable.decimalsicon,R.drawable.fractionsicon,
        R.drawable.algebraicon,R.drawable.algebraicon,
        R.drawable.powersicon,R.drawable.algebraicon,
        R.drawable.coordsicon,R.drawable.algebraicon,
    }; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subjects);
        GridView gridView = (GridView) findViewById(R.id.grid_view); 
        // Instance of ImageAdapter Class 
        gridView.setAdapter(new ImageAdapter(this,mThumbIds)); 
 /**      * On Click event for Single Gridview Item      * */  
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                switch(position)
                {
                    case 0:
                        Intent login = new Intent(getApplicationContext(), MathsAlgebraActivity.class);
                        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(login);
                        break;
                    case 1:
                        Intent coords = new Intent(getApplicationContext(), MathsMultiActivity.class);
                        coords.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(coords);
                        break;
                    case 2:
                        Intent decim = new Intent(getApplicationContext(), MathsMultiActivity.class);
                        decim.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(decim);
                        break;
                    //and so on for other activities.
                    default:
                        break;
                }
            }
        }); 
    }
}

以下は、画像を処理するイメージ アダプターです。

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

private Context mContext;
private Integer[] resIds; 

//Constructor 
public ImageAdapter(Context c,Integer[] resIds){ 
    mContext = c;
    this.resIds=resIds;
}
@Override
public int getCount() {  
    return resIds.length;
}
@Override
public Object getItem(int position) {
    return resIds[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(resIds[position]);  
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);   
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70)); 
    return imageView;
}
}

ケース 0 は実行できますが、1 または 2 は実行できません。

ログからのエラー:

    Thread [<1> main] (Suspended (exception ActivityNotFoundException)) 
Instrumentation.checkStartActivityResult(int, Object) line: 1405    
Instrumentation.execStartActivity(Context, IBinder, IBinder, Activity, Intent, int) line: 1379  
MathsSubjectsActivity(Activity).startActivityForResult(Intent, int) line: 2827  
MathsSubjectsActivity(Activity).startActivity(Intent) line: 2933    
MathsSubjectsActivity$1.onItemClick(AdapterView, View, int, long) line: 51  
GridView(AdapterView).performItemClick(View, int, long) line: 284   
AbsListView$PerformClick.run() line: 1812   
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 130 
ActivityThread.main(String[]) line: 3683    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line:     not available [native method]  
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 839  
ZygoteInit.main(String[]) line: 597 
NativeStart.main(String[]) line: not available [native method]  
4

2 に答える 2

0

編集1

    import android.app.Activity; 
        import android.content.Intent; 
        import android.os.Bundle; 
        import android.view.View; 
        import android.widget.AdapterView;
        import android.widget.AdapterView.OnItemClickListener; 
        import android.widget.GridView;  
        public class MathsSubjectsActivity extends Activity {   

        // Keep all Images in array
        public Integer[] mThumbIds = {        
        R.drawable.algebraicon,R.drawable.coordsicon,         
        R.drawable.decimalsicon,R.drawable.fractionsicon,  
        R.drawable.algebraicon,R.drawable.algebraicon,
        R.drawable.powersicon,R.drawable.algebraicon,        
        R.drawable.coordsicon,R.drawable.algebraicon, 
        }; 


         @Override    
         public void onCreate(Bundle savedInstanceState) {     
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.subjects);  
        GridView gridView = (GridView) findViewById(R.id.grid_view); 
             // Instance of ImageAdapter Class    
        gridView.setAdapter(new ImageAdapter(this,mThumbIds)); 
             /**      * On Click event for Single Gridview Item      * */  
        gridView.setOnItemClickListener(new OnItemClickListener() {  

        @Override         
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {            
         switch(position)             
               {   
                case 0:                 
                   startActivity(this, AlgebraActivity.class);            
                        break;                    
                case 1:                   
                 startActivity(this, CoordsActivity.class);  
                                  break;    

                case 2: 
                startActivity(this,DecimalsActivity.class);
                break;
               //and so on for other activities.  
                default:
                break;                             

 } 


        }     
    }); 
    }   

} 

そして、アダプタは次のようになります。

import android.content.Context;   
import android.view.View; 
import android.view.ViewGroup;  
import android.widget.BaseAdapter; 
import android.widget.GridView;  
import android.widget.ImageView;       
 public class ImageAdapter extends BaseAdapter {    
private Context mContext;      
private Integer[] resIds; 
//Constructor 
 public ImageAdapter(Context c,Integer[] resIds){ 
mContext = c;    
this.resIds=resIds
}    
@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);   
 imageView.setLayoutParams(new GridView.LayoutParams(70, 70)); 
 return imageView;                    
 }                                 
  }   

編集2:

ログによると、manfestファイルにアクティビティのエントリがないようです。androidmanifest.xmlのアクティビティに対して次のエントリを実行します

<activity android:name="com.example.project.YourActivity"
                  . . . >
            . . .
        </activity>
于 2012-04-19T10:59:25.370 に答える
0

以下のコードのようなものを試してください

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

         switch(position)
         {
             case 0:
             startActivity(context, FirstActivity.class);
             break;
             case 1:
             startActivity(context, SecondActivity.class);
             break;
             .....
             ...
         }
    }
});
于 2012-04-19T10:41:57.093 に答える