我想制作一个图像视图应用程序,但我无法全屏显示图像。我可以从SD卡加载它,但是当我点击图像时,应用程序崩溃了。任何人都可以帮助我吗?
这是我的代码:
public class MainActivity extends Activity {
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
ArrayList<Integer> resultList = getIntegerArray(f);
File[] listFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFromSdcard();
GridView imagegrid = (GridView) findViewById(R.id.ImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagegrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
我的FullImageActivity
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter();
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(resultList.get(position));
}
}
我的LogCat错误消息
06-19 11:41:28.456: E/AndroidRuntime(12723): FATAL EXCEPTION: main
06-19 11:41:28.456: E/AndroidRuntime(12723): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.testandroid4/com.example.testandroid4.MainActivity$FullImageActivity}; have you declared this activity in your AndroidManifest.xml?
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Activity.startActivityForResult(Activity.java:3370)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Activity.startActivityForResult(Activity.java:3331)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Activity.startActivity(Activity.java:3566)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.Activity.startActivity(Activity.java:3534)
06-19 11:41:28.456: E/AndroidRuntime(12723): at com.example.testandroid4.MainActivity$1.onItemClick(MainActivity.java:61)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.widget.AbsListView$1.run(AbsListView.java:3423)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.os.Handler.handleCallback(Handler.java:725)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.os.Looper.loop(Looper.java:137)
06-19 11:41:28.456: E/AndroidRuntime(12723): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 11:41:28.456: E/AndroidRuntime(12723): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:41:28.456: E/AndroidRuntime(12723): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:41:28.456: E/AndroidRuntime(12723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 11:41:28.456: E/AndroidRuntime(12723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 11:41:28.456: E/AndroidRuntime(12723): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在
上使用此代码 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
并在Android清单中为FullImageActivity
使用此代码android:theme="@android:style/Theme.Black.NoTitleBar"
或者 在xml中尝试这个,如
android:layout_width="fill_parent"
android:layout_height="fill_parent"
for full_image_view
答案 1 :(得分:0)
您忘记在清单文件中声明您的活动。
您需要在 Manifest.xml :
中添加此内容<activity android:name="com.example.testandroid4.MainActivity$FullImageActivity" />
答案 2 :(得分:0)
我没看到你在FullImageActivity中定义了哪个resultList。我看到它在MainActivity中定义,以及如何使用getIntegerArray(f)填充它(我猜你正在这样做,因为代码不在这里)但我在FullImageActivity上看不到这个数组的定义。
我也在想,如果你有一个id的数组(你的项目中的drawables)。为什么不直接将drawable id从您拥有数组的MainActivity传递给FullImageActivity?,如下所示:
imagegrid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id",resultList.get(position));
startActivity(i);
}
});
然后在FullImageActivity上......:
// get extras in a bundle
Bundle bundle = getIntent().getExtras();
//Create the variable where we are going o store the values passed on the array from MainActivity
int imageResorceId=0;
// Check if the bundle is null
if(bundle!=null){
imageResorceId = bundle.getInt("id",0);
}
//Set the imageView where we will display the drawable
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
//Make sure it is not 0, because if the drawable id is 0, then there was a problem retrieving the value of the array resultList on MainActivity
if(imageResorceId!=0){
imageView.setImageResource(imageResorceId);
}
如果你想为你的FullImageActivity实现一个ViewPager,你需要位置和元素数组,但如果你不想那样......为什么要这么麻烦? ;)