需要帮助获取网格视图图像以打开新活动

时间:2015-05-30 01:32:04

标签: java android gridview android-adapter buttonclick

当我点击按钮时,我尝试编写代码来创建新活动。按钮实际上是使用适配器在网格视图中充气的图片。但由于某种原因,我得到致命的错误异常。 这是我的网格布局页面的代码:

package com.example.yueat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class Grid extends Activity {

protected static Context context;
protected static int pos;

// protected static double buttonID;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridlayout);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            pos = position;
            Intent descr = new Intent("com.example.yueat.DESCR");
            startActivity(descr);
        }
    });
}

protected int getPos() {
    return pos;
}

protected void onPause() {
    super.onPause();
}
}

这是我想要在网格布局中单击图像时打开的页面的代码,每个页面应该打开相同的活动。我希望活动(Descr)显示网格中单击图像的位置在textView。

package com.example.yueat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Descr extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.descr_layout);
        String convert = Double.toString(Grid.pos);
        TextView tv = (TextView)findViewById(R.id.descrText1);
        tv.setText(convert);
}}

当我编译它时,代码在单击图像时崩溃,并且它在Descr类中没有读取任何内容。任何提示/建议/帮助将不胜感激。

LOGCAT错误:

05-29 22:53:17.142: W/dalvikvm(23317): threadid=1: thread exiting with uncaught exception (group=0x4203cda0)
05-29 22:53:17.142: E/AndroidRuntime(23317): FATAL EXCEPTION: main
05-29 22:53:17.142: E/AndroidRuntime(23317): Process: com.example.yueat, PID: 23317
05-29 22:53:17.142: E/AndroidRuntime(23317): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.yueat/com.example.yueat.Descr}; have you declared this activity in your AndroidManifest.xml?
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1636)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Activity.startActivityForResult(Activity.java:3532)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Activity.startActivityForResult(Activity.java:3493)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Activity.startActivity(Activity.java:3735)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.Activity.startActivity(Activity.java:3703)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at com.example.yueat.Grid$1.onItemClick(Grid.java:39)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.widget.AdapterView.performItemClick(AdapterView.java:308)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.widget.AbsListView.performItemClick(AbsListView.java:1495)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3453)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.widget.AbsListView$3.run(AbsListView.java:4816)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.os.Handler.handleCallback(Handler.java:733)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.os.Looper.loop(Looper.java:146)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at android.app.ActivityThread.main(ActivityThread.java:5487)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at java.lang.reflect.Method.invokeNative(Native Method)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at java.lang.reflect.Method.invoke(Method.java:515)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
05-29 22:53:17.142: E/AndroidRuntime(23317):    at dalvik.system.NativeStart.main(Native Method)
05-29 22:58:17.162: I/Process(23317): Sending signal. PID: 23317 SIG: 9
05-29 22:58:17.442: I/PersonaManager(23779): getPersonaService() name persona_policy
05-29 22:58:17.502: I/PersonaManager(23779): getPersonaService() name persona_policy
05-29 22:58:17.593: I/PersonaManager(23779): getPersonaService() name persona_policy
05-29 22:58:17.673: D/AbsListView(23779): Get MotionRecognitionManager
05-29 22:58:17.703: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.703: D/MyAdapter(23779): getItem successful
05-29 22:58:17.703: D/MyAdapter(23779): getItem successful
05-29 22:58:17.743: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.743: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.743: D/MyAdapter(23779): layout inflater successful
05-29 22:58:17.743: D/MyAdapter(23779): view row successful
05-29 22:58:17.743: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.743: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.743: D/MyAdapter(23779): view row successful
05-29 22:58:17.743: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.743: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.743: D/MyAdapter(23779): view row successful
05-29 22:58:17.773: W/Adreno-GSL(23779): <get_panel_settings:3802>: Android framework reported version 2. So, don't force ES30
05-29 22:58:17.773: I/Adreno-EGL(23779): <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.00.007.018_msm8960_KK_2.7_RB1_CL3869936_release_AU (CL3869936)
05-29 22:58:17.773: I/Adreno-EGL(23779): OpenGL ES Shader Compiler Version: 17.01.10.SPL
05-29 22:58:17.773: I/Adreno-EGL(23779): Build Date: 12/09/13 Mon
05-29 22:58:17.773: I/Adreno-EGL(23779): Local Branch: mybranch2092625
05-29 22:58:17.773: I/Adreno-EGL(23779): Remote Branch: quic/kk_2.7_rb1.15
05-29 22:58:17.773: I/Adreno-EGL(23779): Local Patches: NONE
05-29 22:58:17.773: I/Adreno-EGL(23779): Reconstruct Branch: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.00.007.018 +  NOTHING
05-29 22:58:17.793: D/OpenGLRenderer(23779): Enabling debug mode 0
05-29 22:58:17.793: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.793: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.793: D/MyAdapter(23779): view row successful
05-29 22:58:17.793: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.793: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.803: D/MyAdapter(23779): layout inflater successful
05-29 22:58:17.803: D/MyAdapter(23779): view row successful
05-29 22:58:17.803: D/context = %s ...(23779): android.app.Application@42a3d060
05-29 22:58:17.803: D/MyAdapter(23779): getItemId successful
05-29 22:58:17.803: D/MyAdapter(23779): layout inflater successful
05-29 22:58:17.803: D/MyAdapter(23779): view row successful

ANDROID MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yueat"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Grid"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.yueat.GRID" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Descr"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.yueat.DESCR" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

onItemClick 中尝试此代码:

 gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        Intent descr = new Intent(Grid.this,Descr.class);
        startActivity(descr);
    }
});

答案 1 :(得分:0)

您需要将网格的位置与意图一起传递。您的Grid类看起来像这样

package com.example.yueat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class Grid extends Activity {

protected static Context context;
protected static int pos;

// protected static double buttonID;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridlayout);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            pos = position;
            Intent descr = new Intent(Grid.this, Descr.class);
            descr.putExtra("position", pos);
            startActivity(descr);
        }
    });
}

protected int getPos() {
    return pos;
}

protected void onPause() {
    super.onPause();
}
}

你的描述类看起来像这样。在这里,您将从Intent获取位置值,然后在TextView中显示它。

package com.example.yueat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Descr extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.descr_layout);
        Intent mIntent = getIntent();
        int position= mIntent.getIntExtra("position", 0);
        TextView tv = (TextView)findViewById(R.id.descrText1);
        tv.setText(position + "");
}}

编辑:将您的Android清单更改为以下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yueat"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Grid"
            android:label="@string/app_name" >

        </activity>
        <activity
            android:name=".Descr"
            android:label="@string/app_name" >

        </activity>
    </application>

</manifest>

答案 2 :(得分:0)

在onItemClick中尝试此代码:

 gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
        int position, long id) {

    Intent descr = new Intent(Grid.this,Descr.class);
    getApplicationContext().startActivity(descr);
}

});