2

这是一个食谱应用程序,我只是在学习,所以如果你能帮我纠正我的错误并解释,我将不胜感激。提前致谢

主要活动

package rice.recipe;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class RiceActivity extends Activity {
    private RiceRecipeDataSource ds;
    private ListView recipelistview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rice);
        ds=new RiceRecipeDataSource();
        recipelistview=(ListView)findViewById(rice.recipe.R.id.listView1);
        recipelistview.setAdapter(new RiceRecipeDataSourceAdapter(this,ds ));


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.rice, menu);
        return true;
    }

}

适配器代码

package rice.recipe;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RiceRecipeDataSourceAdapter extends BaseAdapter {


        private Context mycontext;
        private LayoutInflater myinflater;
        private RiceRecipeDataSource mydatasource;

        public RiceRecipeDataSourceAdapter(Context ctx,RiceRecipeDataSource ds)
        {
            mycontext=ctx;
            mydatasource=ds;
            myinflater=(LayoutInflater)mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mydatasource.getDataSourceLength();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView thumbnail;
            TextView name;

            if (convertView==null)
                convertView=myinflater.inflate(rice.recipe.R.layout.activity_rice, parent,false);
            thumbnail=(ImageView)convertView.findViewById(rice.recipe.R.id.imageView1);
            thumbnail.setImageResource(mydatasource.getPhotoPool().get(position));
            name=(TextView)convertView.findViewById((rice.recipe.R.id.textView1));
            name.setText(mydatasource.getDishesPool().get(position));

            return convertView;

        }

    }

数据源代码

package rice.recipe;


import java.util.ArrayList;

public class RiceRecipeDataSource
{
  private ArrayList<Integer> photoPool;
  private ArrayList<Integer> descriptionPool;
  private ArrayList<Integer> dishesPool;


public ArrayList<Integer> getPhotoPool() 
{
    return photoPool;
}
public ArrayList<Integer> getDescriptionPool()
{
    return descriptionPool;
}
public ArrayList<Integer> getDishesPool() 
{
    return dishesPool;
}
public RiceRecipeDataSource()
{
photoPool=new ArrayList<Integer>();
descriptionPool=new ArrayList<Integer>();
dishesPool=new ArrayList<Integer>();

setupPhotopool();
setupDescriptionpool();
setupDishespool();
}
public void setupPhotopool()
{

    photoPool.add(rice.recipe.R.drawable.jollofrice);
    photoPool.add(rice.recipe.R.drawable.friedrice);
    photoPool.add(rice.recipe.R.drawable.spaghetti);
    photoPool.add(rice.recipe.R.drawable.coconutrice);
    photoPool.add(rice.recipe.R.drawable.ricenbeans);
    photoPool.add(rice.recipe.R.drawable.whiterice);

}

public void setupDishespool()
{
    dishesPool.add(rice.recipe.R.string.jellof);
    dishesPool.add(rice.recipe.R.string.fried_rice);
    dishesPool.add(rice.recipe.R.string.spag);
    dishesPool.add(rice.recipe.R.string.coconut);
    dishesPool.add(rice.recipe.R.string.rice_beans);
    dishesPool.add(rice.recipe.R.string.plain_rice);

}

public void setupDescriptionpool()
{
    descriptionPool.add(rice.recipe.R.string.jellof_desc);
    descriptionPool.add(rice.recipe.R.string.friedr_desc);
    descriptionPool.add(rice.recipe.R.string.spag_desc);
    descriptionPool.add(rice.recipe.R.string.coconut_desc);
}

public int getDataSourceLength()
{
    return photoPool.size();

}
}

这是我的 LogCat

04-03 05:47:34.217: D/dalvikvm(865): GC_FOR_ALLOC freed 39K, 6% free 2657K/2816K, paused 78ms, total 83ms
04-03 05:47:34.237: I/dalvikvm-heap(865): Grow heap (frag case) to 3.783MB for 1127536-byte allocation
04-03 05:47:34.317: D/dalvikvm(865): GC_FOR_ALLOC freed 2K, 5% free 3756K/3920K, paused 80ms, total 80ms
04-03 05:47:34.421: D/dalvikvm(865): GC_CONCURRENT freed <1K, 5% free 3760K/3920K, paused 13ms+20ms, total 108ms
04-03 05:47:34.697: D/AndroidRuntime(865): Shutting down VM
04-03 05:47:34.697: W/dalvikvm(865): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 05:47:34.778: E/AndroidRuntime(865): FATAL EXCEPTION: main
04-03 05:47:34.778: E/AndroidRuntime(865): java.lang.NullPointerException
04-03 05:47:34.778: E/AndroidRuntime(865):  at rice.recipe.RiceRecipeDataSourceAdapter.getView(RiceRecipeDataSourceAdapter.java:52)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.ListView.onMeasure(ListView.java:1158)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.View.measure(View.java:15518)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.View.measure(View.java:15518)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.View.measure(View.java:15518)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.View.measure(View.java:15518)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 05:47:34.778: E/AndroidRuntime(865):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.View.measure(View.java:15518)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.Choreographer.doFrame(Choreographer.java:532)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.os.Handler.handleCallback(Handler.java:725)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.os.Looper.loop(Looper.java:137)
04-03 05:47:34.778: E/AndroidRuntime(865):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 05:47:34.778: E/AndroidRuntime(865):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 05:47:34.778: E/AndroidRuntime(865):  at java.lang.reflect.Method.invoke(Method.java:511)
04-03 05:47:34.778: E/AndroidRuntime(865):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-03 05:47:34.778: E/AndroidRuntime(865):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-03 05:47:34.778: E/AndroidRuntime(865):  at dalvik.system.NativeStart.main(Native Method)

清单文件

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="rice.recipe.RiceActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.main" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

编辑后,新的logcat

04-03 07:23:36.608: D/dalvikvm(933): GC_FOR_ALLOC freed 46K, 6% free 2657K/2824K, paused 175ms, total 179ms
04-03 07:23:36.631: I/dalvikvm-heap(933): Grow heap (frag case) to 3.783MB for 1127536-byte allocation
04-03 07:23:36.709: D/dalvikvm(933): GC_FOR_ALLOC freed 2K, 5% free 3756K/3928K, paused 75ms, total 75ms
04-03 07:23:36.858: D/dalvikvm(933): GC_CONCURRENT freed <1K, 4% free 3771K/3928K, paused 13ms+19ms, total 153ms
04-03 07:23:37.161: D/AndroidRuntime(933): Shutting down VM
04-03 07:23:37.161: W/dalvikvm(933): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 07:23:37.248: E/AndroidRuntime(933): FATAL EXCEPTION: main
04-03 07:23:37.248: E/AndroidRuntime(933): java.lang.NullPointerException
04-03 07:23:37.248: E/AndroidRuntime(933):  at rice.recipe.RiceRecipeDataSourceAdapter.getView(RiceRecipeDataSourceAdapter.java:52)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.ListView.onMeasure(ListView.java:1158)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.View.measure(View.java:15518)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.View.measure(View.java:15518)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.View.measure(View.java:15518)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.View.measure(View.java:15518)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 07:23:37.248: E/AndroidRuntime(933):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.View.measure(View.java:15518)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.Choreographer.doFrame(Choreographer.java:532)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.os.Handler.handleCallback(Handler.java:725)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.os.Looper.loop(Looper.java:137)
04-03 07:23:37.248: E/AndroidRuntime(933):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 07:23:37.248: E/AndroidRuntime(933):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 07:23:37.248: E/AndroidRuntime(933):  at java.lang.reflect.Method.invoke(Method.java:511)
04-03 07:23:37.248: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-03 07:23:37.248: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-03 07:23:37.248: E/AndroidRuntime(933):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您没有正确遵循适配器自定义。检查这个。您的 getView 应该类似于给定的代码片段。这个也可以查

于 2013-04-03T06:53:03.050 回答