0

我想做一个图像查看应用程序,但我无法全屏显示图像。我可以从 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)
4

3 回答 3

0

您忘记在清单文件中声明您的活动。

您需要在Manifest.xml中添加它:

<activity android:name="com.example.testandroid4.MainActivity$FullImageActivity" />
于 2013-06-19T10:19:41.223 回答
0

我没有看到您在 FullImageActivity 中定义了 resultList。我看到它在 MainActivity 中定义,以及你如何用 getIntegerArray(f) 填充它(我猜你做对了,因为代码不在这里)但我看不到这个数组在 FullImageActivity 上定义。

我也在想,如果你有一个 id 数组(项目中的可绘制对象)。为什么不将可绘制 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 ,你需要元素的位置和数组但是如果你不想要那个......为什么还要麻烦?;)

于 2013-06-19T12:24:59.580 回答
0

使用此代码

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

并在 android manifest 中为 FullImageActivity 使用此代码

android:theme="@android:style/Theme.Black.NoTitleBar"

或者在xml中试试这个

android:layout_width="fill_parent"
android:layout_height="fill_parent"

对于 full_image_view

于 2013-06-19T09:26:30.283 回答