1

所以我搜索和搜索,我没有找到任何像我正在寻找的东西。我有几个 ListActivities,我可以单击一个并转到下一个列表和下一个列表等...我想要的是当我单击相应的列表项时,我希望它显示 res 文件夹中的图像。我整个下午都被困在这个问题上,我找不到任何工作。任何帮助都会很棒。示例何时选择覆盖物,它检索封面_sheet.png。谢谢詹姆斯

应用程序的入口点:Menu.java

public class Menu extends ListActivity 
{ 
    public static final String EXTRA_PICTURE = "extra.picture.toshow"; 

    String classes[] = { "CoverSheet",// "TreatmentProtocols", "EMSProcedures",
           // "DrugList", "EMSPolicies", "EMSTriageAndDestinationPlan",  
           // "Appendices"
            }; 
    //in this array you store the drawable resource ids to show.  
    //you can change it to hold urls or other identifiers. 
    int images[] = { R.drawable.cover_sheet, }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setListAdapter(new ArrayAdapter<String>(Menu.this,  
                android.R.layout.simple_list_item_1, classes)); 
    } 

    protected void onCreate1(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.imageview); 
        final int imgResource = getIntent().getIntExtra(Menu.EXTRA_PICTURE, 0);
        final ImageView img = (ImageView)findViewById(R.id.img); 
        img.setImageResource(imgResource); }

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) 
    { 
        super.onListItemClick(l, v, position, id); 
        String Menu = classes[position]; 
        try 
        { 
            Class firstClass = Class.forName("protocols.NashCounty." + Menu); 
            Intent firstIntent = new Intent(Menu.this, firstClass); 
            firstIntent.putExtra(EXTRA_PICTURE, images[position]); 
            startActivity(firstIntent); 
        } 
        catch (ClassNotFoundException e) 
        { 
            Log.e("SAMPLE", "ClassNotFoundException ", e); 
        } 
        catch (Exception e) 
        { 
            Log.e("SAMPLE", "Exception ", e); 
        } 
    } 
} 

这是我创建的班级

public class CoverSheet extends ListActivity
{
    public static final String EXTRA_PICTURE = "extra.picture.toshow";
    String classes[] = { "CoverSheet", };
    // in this array you store the drawable resource ids to show.
    // you can change it to hold urls or other identifiers.
    int images[] = { R.drawable.cover_sheet };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview);
        final int imgResource = getIntent().getIntExtra(
                CoverSheet.EXTRA_PICTURE, 0);
        final ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageResource(imgResource);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        String coversheet = classes[position];
        try
        {
            Class firstClass = Class.forName("protocols.NashCounty."+classes);
            Intent firstIntent = new Intent(CoverSheet.this, firstClass);
            firstIntent.putExtra(EXTRA_PICTURE, images[position]);
            startActivity(firstIntent);
        }
        catch (ClassNotFoundException e)
        {
            Log.e("SAMPLE", "ClassNotFoundException ", e);
        }
        catch (Exception e)
        {
            Log.e("SAMPLE", "Exception ", e);
        }
    }
}

下面是我的 xml 布局 l 名为 imageview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView android:id="@+id/img"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/cover_sheet" />
</LinearLayout>

下面是我的minifest

<activity android:name=".activity.CoverSheet"  
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> 
</application>
</manifest>

我误解了你之前说的内容,对此我深表歉意,我想我开始不理解它了……我想。

以下是我尝试运行代码时出现的 logcat 错误:

05-18 17:15:26.221:E/AndroidRuntime(618):致命异常:主要 05-18 17:15:26.221:E/AndroidRuntime(618):java.lang.ArrayIndexOutOfBoundsException:长度=1;index=3 05-18 17:15:26.221: E/AndroidRuntime(618): 在协议.NashCounty.Menu.onListItemClick(Menu.java:40) 05-18 17:15:26.221: E/AndroidRuntime(618):在 android.app.ListActivity$2.onItemClick(ListActivity.java:319) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AdapterView.performItemClick(AdapterView.java:292) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView.performItemClick(AbsListView.java:1058) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView $PerformClick.run(AbsListView.java:2514) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView$1.run(AbsListView.java:3168) 05-18 17:15: 26.221:

4

1 回答 1

0

classes我假设您在变量中列出名称的活动中显示图像。

确保列出的所有类也包含在androidManifest.xml正确的<activity />标记和正确的包声明中!

您对变量和包名称的使用具有误导性,但我认为您的应用程序包是protocols.NashCountry,因此在清单中应该有类似于以下的一行:

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

对于您要调用的每个活动课程。

您还应该尝试更改

e.printStackTrace();

进入

Log.e("SAMPLE", "ClassNotFoundException ", e);

编辑:要使您的后续活动全屏显示,您需要android:theme在清单中设置属性:

<activity android:name=".activity.CoverSheet" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".activity.TreatmentProtocols" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

下面的代码显示了如何处理Intent额外的:

public class Menu extends ListActivity
{
    public static final String EXTRA_PICTURE = "extra.picture.toshow";
    String classes[] = { "CoverSheet", "TreatmentProtocols", "EMSProcedures", 
            "DrugList", "EMSPolicies", "EMSTriageAndDestinationPlan", 
            "Appendices" };
    //in this array you store the drawable resource ids to show. 
    //you can change it to hold urls or other identifiers.
    int images[] = { R.drawable.coversheet, R.drawable.img2, R.drawable.img3, 
        R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, 
                android.R.layout.simple_list_item_1, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        String Menu = classes[position];
        try
        {
            Class firstClass = Class.forName("protocols.NashCounty." + Menu);
            Intent firstIntent = new Intent(Menu.this, firstClass);
            firstIntent.putExtra(EXTRA_PICTURE, images[position]);
            startActivity(firstIntent);
        }
        catch (ClassNotFoundException e)
        {
            Log.e("SAMPLE", "ClassNotFoundException ", e);
        }
        catch (Exception e)
        {
            Log.e("SAMPLE", "Exception ", e);
        }
    }
}

最后,onCreate您的类中的方法应该根据extra提供给它们的图像显示正确的图像:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_img);
    final int imgResource = getIntent().getIntExtra(Menu.EXTRA_PICTURE, 0);
    final ImageView img = (ImageView)findViewById(R.id.img);
    img.setImageResource(imgResource);
}

请注意,如果您只需要根据列表项点击显示不同的图像,则不需要单独的活动,也不必通过名称访问它们:您应该使用单个活动,并传递 id启动意图时将图像作为额外参数。

编辑:您的CoverSheet活动课程应如下所示:

public class CoverSheet extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview);
        final int imgResource = getIntent().getIntExtra(Menu.EXTRA_PICTURE, 0);
        final ImageView img = (ImageView)findViewById(R.id.img);
        img.setImageResource(imgResource);
    }
}

很简单,不需要扩展ListActivity,因为你不想处理那里的任何列表,你只想显示一张图片。

其他变量对于这种情况也无用!

编辑 2:您的onListItemClick方法也应该变得更简单!不需要动态检索类,因此您也可以从活动中完全删除classes数组变量。Menu

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    try
    {
        Intent firstIntent = new Intent(Menu.this, CoverSheet.class);
        firstIntent.putExtra(EXTRA_PICTURE, images[position]);
        startActivity(firstIntent);
    }
    catch (Exception e)
    {
        Log.e("SAMPLE", "Exception ", e);
    }
}
于 2012-05-14T05:29:28.613 回答