0

我在开发的应用程序中有一些讨厌的错误,基本上在主要活动中我有两个列表视图和一个页面适配器,它们从网络加载数据 - 保存到 sql - 然后显示在视图上,

所有功能都很好,只是在向 viewPager 显示图像时出现了一个问题,它说 NullPointerException

这是显示图像适配器http://tny.cz/04b77f28

这是适配器代码

private class pageAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return JML_SLIDE;
    }

    @Override
    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View p = inflater.inflate(R.layout.list_headline_home, null);
        Cursor cursor;
        cursor = managedQuery(Uri.withAppendedPath(RolProvider.CONTENT_URI,
                Database.Project.NAME),
                new String[] { BaseColumns._ID,
                        Database.Project.THE_TITLE,
                        Database.Project.THE_BIGIMAGE }, null, null, null);
        cursor.moveToPosition(position);
        TextView judul = (TextView) p.findViewById(R.id.headline_title);
        judul.setText(cursor.getString(1));
         ImageView img = (ImageView) p.findViewById(R.id.headline_img);
         String imageUrl = cursor.getString(2);
         img.setTag(imageUrl);
//line:299   loader.DisplayImage(imageUrl, (Activity) p.getContext(), img);

         btnAction(cursor.getPosition());

        ((ViewPager) pager).addView(p, 0);

        return p;

    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((View) object);
    }

    @Override
    public void finishUpdate(View arg0) {
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
    }

}

private void btnAction(int action) {
    for (int i = 0; i < 5; i++) {
        String buttonA = "btn" + Integer.toString(i + 1);
        int resID = getResources().getIdentifier(buttonA, "id",
                getPackageName());
        Button buttonsIA = ((Button) findViewById(resID));
        setButton(buttonsIA, Integer.toString(i + 1), 5, 5);
    }

    String buttonID = "btn" + Integer.toString(action + 1);
    int resID = getResources().getIdentifier(buttonID, "id",
            getPackageName());
    Button buttonsI = ((Button) findViewById(resID));
    setButton(buttonsI, Integer.toString(action + 1), 10, 10);

}

private void setButton(Button btn, String text, int h, int w) {

    btn.setWidth(w);
    btn.setHeight(h);
//  btn.setText(text);
}

这是我在寻呼机中使用的 xml

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

    <ImageView
        android:id="@+id/headline_img"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/stub">
    </ImageView>

    <TextView
        android:id="@+id/headline_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/sample_image"
        android:layout_alignParentLeft="true"
        android:background="#AA000000"
        android:padding="5dp"
        android:textColor="#ffffffff"
        android:textSize="12dp" >
    </TextView>

    <TextView
        android:id="@+id/sample_title22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/sample_title"
        android:layout_alignParentLeft="true"
        android:background="#AA000000"
        android:padding="5dp"
        android:textColor="#ffffffff"
        android:textSize="12dp" >
    </TextView>
</RelativeLayout>

这是logcat说的

04-22 17:05:00.578: E/AndroidRuntime(280): java.lang.NullPointerException
04-22 17:05:00.578: E/AndroidRuntime(280):  at com.Myapp.mobile.MainActivity$pageAdapter.instantiateItem(MainActivity.java:299)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.view.View.measure(View.java:8171)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-22 17:05:00.578: E/AndroidRuntime(280):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)

任何线索怎么会发生?提前感谢您的所有帮助

更新了活动类:

public class MainActivity extends SherlockActivity implements
    OnItemClickListener {

public static ProgressDialog progressDialog;
private ViewPager pager;
private static int JML_SLIDE = 5;
private pageAdapter adaptador;
ImageLoader loader = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    setContentView(R.layout.act_main);


    adaptador = new pageAdapter();
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adaptador);
    pager.setCurrentItem(0);
    btnAction(0);

    Button load_btn = (Button) findViewById(R.id.btn_terpopuler);
    load_btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getTerpopuler();
        }
    });

}
4

0 回答 0