我碰到了一堵砖墙,我遵循的教程只是在点击时放大了图像。
我想做的是为每个图像打开一个不同的活动。
例如,代数图像打开代数活动,点击十进制图像打开十进制活动等。
这可能很简单,但目前对我来说并不是很明显。
这里是主要活动。
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;
}
}
可以运行 case 0 但不能运行 case 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]