Java 为什么我进入页面时不能按按钮?
在添加网格视图布局之前,我能够使用按钮功能。但是,在插入网格视图布局后,我所能做的就是在网格视图上滚动图像。我该怎么做才能既能滚动图像又能使用按钮?谢谢 公营班级流动电话推广活动{Java 为什么我进入页面时不能按按钮?,java,android,button,gridview,Java,Android,Button,Gridview,在添加网格视图布局之前,我能够使用按钮功能。但是,在插入网格视图布局后,我所能做的就是在网格视图上滚动图像。我该怎么做才能既能滚动图像又能使用按钮?谢谢 公营班级流动电话推广活动{ public class Uploads extends Activity implements OnClickListener { ImageView btnAllShops, btnFavourites, btnUploads, btnSettings, btnBuys, btn
public class Uploads extends Activity implements OnClickListener {
ImageView btnAllShops, btnFavourites, btnUploads, btnSettings, btnBuys,
btnTakePhoto;
GridView gvUploads;
PhotoDbAdapter ourHelper;
private String description, category, price, imagepath;
final static int cameraData = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_uploads);
findViewById();
onBackPressed();
sdcard();
}
private void findViewById() {
gvUploads = (GridView) findViewById(R.id.gvUploads);
btnAllShops = (ImageView) findViewById(R.id.btnAllShops);
btnFavourites = (ImageView) findViewById(R.id.btnFavourites);
btnUploads = (ImageView) findViewById(R.id.btnUploads);
btnSettings = (ImageView) findViewById(R.id.btnSettings);
btnBuys = (ImageView) findViewById(R.id.btnBuys);
btnTakePhoto = (ImageView) findViewById(R.id.btnTakePhoto);
btnAllShops.setOnClickListener(this);
btnFavourites.setOnClickListener(this);
btnUploads.setOnClickListener(this);
btnSettings.setOnClickListener(this);
btnBuys.setOnClickListener(this);
btnTakePhoto.setOnClickListener(this);
}
@Override
public void onBackPressed() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
Intent goMain = new Intent(getApplicationContext(),
EditUploads.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bs);
goMain.putExtra("byteArray", bs.toByteArray());
startActivity(goMain);
}
}
private void sdcard() {
setContentView(R.layout.activity_uploads);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which
// columns
// to
// return
null, // Return all rows
null, MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView gvuploads = (GridView) findViewById(R.id.gvUploads);
gvuploads.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
picturesView.setPadding(10, 10, 10, 10);
picturesView
.setLayoutParams(new GridView.LayoutParams(266, 266));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}
private Cursor cursor;
// Column index for the Thumbnails Image IDs.
private int columnIndex;
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnAllShops:
Intent iA = new Intent(getApplicationContext(), AllShops.class);
startActivity(iA);
break;
case R.id.btnFavourites:
Intent iF = new Intent(getApplicationContext(), Favourites.class);
startActivity(iF);
break;
case R.id.btnUploads:
Intent iU = new Intent(getApplicationContext(), Uploads.class);
startActivity(iU);
break;
case R.id.btnSettings:
Intent iS = new Intent(getApplicationContext(),
SettingsActivity.class);
startActivity(iS);
break;
case R.id.btnBuys:
Intent iBuy = new Intent(getApplicationContext(), Buys.class);
startActivity(iBuy);
break;
case R.id.btnTakePhoto:
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
}
}
如果图像进入网格,这可能会有帮助:
gridview.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
如果(v.getId()==btnFavourites.getId()){
//有事发生
}
}
});代码>您是否已将布局包含在其他布局中,或者可能存在上下文问题。这是另一个同样有gridview布局的类。如何检查是否存在上下文问题?调试代码并想知道代码停在何处。刚刚尝试过,但没有出现任何错误显示包含网格视图和按钮的xml布局。如果您可以进一步解释这是如何工作的,以及我应该将这行代码大致放在何处,可以吗?抱歉..我对这些东西还不太熟悉..提前谢谢Corry,我刚开始使用StackOverflow,但是你可以试试这个:当我运行应用程序时,当我点击页面时,它就会崩溃。我检查了logcat,它说用SetonicClickListener代替。这是个好主意吗?试着用你的方法,但仍然不起作用。无论如何,非常感谢
PhotoDbAdapter ourHelper;
ImageView grid_item_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mobile);
grid_item_image = (ImageView) findViewById(R.id.grid_item_image);
PhotoDbAdapter getImage = new PhotoDbAdapter(this);
getImage.open();
String getImagepathe = getImage.getImagePath();
Toast.makeText(this, getImagepathe, Toast.LENGTH_LONG).show();
int id = getResources().getIdentifier(getImagepathe, null, null);
grid_item_image.setImageResource(id);
Intent mobile = new Intent(getApplicationContext(), Uploads.class);
startActivity(mobile);
}
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:src="@drawable/sh_uploads" />
<ImageView
android:id="@+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/hbtn_camera" />
</LinearLayout>
<GridView
android:id="@+id/gvUploads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottomBar"
android:layout_below="@+id/linearLayout1"
android:numColumns="3"
tools:listitem="@layout/mobile" >
</GridView>
<LinearLayout
android:id="@+id/bottomBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/btnAllShops"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:contentDescription="@null"
android:src="@drawable/sbtn_home" />
<ImageView
android:id="@+id/btnFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:contentDescription="@null"
android:src="@drawable/sbtn_fave" />
<ImageView
android:id="@+id/btnBuys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:contentDescription="@null"
android:src="@drawable/sbtn_buys" />
<ImageView
android:id="@+id/btnUploads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:contentDescription="@null"
android:src="@drawable/sbtn_uploads" />
<ImageView
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:contentDescription="@null"
android:src="@drawable/sbtn_settings" />
</LinearLayout>