我是android的新手,我正在尝试从网格视图创建一个视图寻呼机,但我在点击网格视图中的项目时遇到了问题。
这是我的代码
ImageAdapter.java
import com.netvariant.qareeboon.android.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
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 mThumbIds[position];
}
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.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
ImagePagerAdapter .java
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImagePagerAdapter extends PagerAdapter {
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
// References to our images in res > drawable
public Integer[] mThumbIds = {
R.drawable.bg_logo, R.drawable.three,
R.drawable.four, R.drawable.two,
R.drawable.one, R.drawable.btn_about,
};
}
ImageViewPager .java
import java.util.ArrayList;
import java.util.List;
import com.netvariant.qareeboon.android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
public class ImageViewPager extends Activity {
// Declare Variable
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);
// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}
活动
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.view.View;
public class InfoGraphicActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);
// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
// ViewPager viewPager =(ViewPager)findViewById(R.id.pager);
gridview.setAdapter(new ImageAdapter(this));
// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);
// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);
// Start ImageViewPager
startActivity(i);
}
});
}
// Not using options menu for this tutorial
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.info_graphic, menu);
return true;
}
}
您可以在下面看到错误日志:
08-21 11:03:38.289: E/AndroidRuntime(5196): FATAL EXCEPTION: main
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
08-21 11:03:38.289: E/AndroidRuntime(5196): at com.netvariant.android.qareeboon.InfoGraphicActivity$1.onItemClick(InfoGraphicActivity.java:40)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-21 11:03:38.289: E/AndroidRuntime(5196): at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.netvariant.qareeboon.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.netvariant.android.qareeboon.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MainMenu"
android:label="@string/title_activity_main_menu"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.EducateActivity"
android:label="@string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ConsultActivity"
android:label="@string/title_activity_consult"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ContactUsActivity"
android:label="@string/title_activity_contact_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.InfoGraphicActivity"
android:label="@string/title_activity_info_graphic"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.qareeboon.android.SettingsActivity"
android:label="@string/title_activity_settings"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.SettingActivity"
android:label="@string/title_activity_setting"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.MyConsultation"
android:label="@string/title_activity_my_consultation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AboutUs"
android:label="@string/title_activity_about_us"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.ArticleActivity"
android:label="@string/title_activity_article"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.Comments"
android:label="@string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.netvariant.android.qareeboon.AddComment"
android:label="@string/title_activity_add_comment"
android:screenOrientation="portrait" >
</activity>
<activity android:name=".ImageViewPager" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
在logcat中看一下这个:
08-21 11:03:38.289: E/AndroidRuntime(5196): android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.netvariant.qareeboon.android/com.netvariant.android.qareeboon.ImageViewPager}; have you declared this activity in your AndroidManifest.xml?
您尚未在应用程序的AndroidManifest
文件中提及该活动。请这样做。
<activity
android:name="com.netvariant.android.qareeboon.ImageViewPager">
</activity>
答案 1 :(得分:0)
请在清单文件中声明您的活动
答案 2 :(得分:0)
您尚未在清单中定义新活动。您正在启动哪个活动,其名称需要在您的应用程序标记之间添加,如下所示:
<activity
android:name=".Activity_Class_Name" />