0

更新 2

基本的に3つのクラスがあります。メインビューにカスタムリストビューがあります。メインアクティビティを開くときにnullポインター例外が発生し、リストが表示されないという問題があります。

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

  <Button android:text="Take (small) Picture" android:id="@+id/btnIntendS"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1" />



</LinearLayout>


 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_below="@+id/pic"
   android:orientation="vertical" >

   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="Choose the Picture or Video to share"
       android:gravity="center_horizontal"
       android:textSize="18dp" />

<LinearLayout
  android:layout_width="match_parent"
  android:id="@+id/imageview"
  android:layout_height="300dp"
  android:layout_gravity="center_vertical"
  android:orientation="vertical" >

<ImageView 
  android:layout_height="wrap_content" 
  android:layout_width="wrap_content" 
  android:layout_marginTop="200dp"
  android:visibility="visible"
  android:id="@+id/imageView1" />

<LinearLayout 
  android:layout_below="@id/imageview"
  android:layout_height="wrap_content"
  android:layout_width = "match_parent"
  android:orientation="horizontal">

  <ListView
      android:id="@+id/list"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:visibility="visible" />
  </LinearLayout>




 </RelativeLayout>

ここにlist_item.xmlがあります

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/icon"
    android:paddingBottom="10dp"
    android:textColor="#CC0033"
    android:textSize="16dp" />

<TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/icon"
    android:paddingLeft="10dp"
    android:textColor="#3399FF"
    android:textSize="14dp" />
  </RelativeLayout>

ここでわかるように、1 つのイメージビューとリスト ビューの 2 つのボタンを持つ xml があります。リストは、2 つのものを取得しているメイン アクティビティから更新されます。1.)小さなテキスト 2.)大きなテキスト

今、私はメイン アクティビティの onCreate メソッドでローカル データベースからこれら 2 つを取得します。

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picactivity);

    mydb = new DBhelper(PicActivity.this);

    try {
        mydb.createDataBase(cont);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("I am in Picactivity");

    mImageView = (ImageView) findViewById(R.id.imageView1);

    mImageBitmap = null;


    Button picBtn = (Button) findViewById(R.id.btnIntend);
    setBtnListenerOrDisable( 
            picBtn, 
            mTakePicOnClickListener,
            MediaStore.ACTION_IMAGE_CAPTURE
    );

    Button picSBtn = (Button) findViewById(R.id.btnIntendS);
    setBtnListenerOrDisable( 
            picSBtn, 
            mTakePicSOnClickListener,
            MediaStore.ACTION_IMAGE_CAPTURE
    );



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        mAlbumStorageDirFactory = new PicAlbumDirFactory();

    } else {
        mAlbumStorageDirFactory = new BaseAlbumDirFactory();        
    }


    mydb.openDataBase();


    images = mydb.getimages();
    description = mydb.getDescriptions();

    location = mydb.getLocations();
    int i = images.size();
    System.out.println("MY DATABASE SIZE"+i);
    int k = 0;
    if(i>0){
    rowItems = new ArrayList<RowItem>();
     for (int j = 0;j<i ; j++) {
         System.out.println("Loop Value:--"+j);
         System.out.println("Location Details:-----"+location.get(j));
         System.out.println("Description Details:------"+description.get(j));

            RowItem item = new RowItem(location.get(j), description.get(j));
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.list);
         CustomListViewAdapter adapter = new CustomListViewAdapter(PicActivity.this,
                R.layout.list_item, rowItems);
         listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

Listview onItemClick Listner メソッドは次のようになります

     @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast toast = Toast.makeText(getApplicationContext(),
            "Item " + (position + 1) + ": " + rowItems.get(position),
            Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }

現在、2 つの Other クラスがあります。1 つは、データを行ごとにリストに格納するための RowItem クラスです。

     public class RowItem {

private String title;
private String desc;

public RowItem( String title, String desc) {

    this.title = title;
    this.desc = desc;
}



public String getDesc() {
    return desc;
}
public void setDesc(String desc) {
    this.desc = desc;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
@Override
public String toString() {
    return title + "\n" + desc;
}
 }

そして最後に私の Customlistview クラス........

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;
Bitmap mybitmap;

public CustomListViewAdapter(Context context, int resourceId,
        List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}


private class ViewHolder {

    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);


    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());

    return convertView;
}
}

最後に私のLogcatビュー..

      09-04 14:11:57.634: I/System.out(16814): Context value:--com.cdotsys.photoapp.PicActivity@421a24a0
      09-04 14:11:57.639: I/System.out(16814): My Storage State:-mounted
      09-04 14:11:57.639: I/System.out(16814): Yes
      09-04 14:11:57.639: I/System.out(16814): My database Path:--/storage/sdcard0/PhotoApp
      09-04 14:11:57.639: I/System.out(16814): I am in Picactivity
      09-04 14:11:57.644: I/System.out(16814): MY DATABASE PATH:----/storage/sdcard0/PhotoApp/photodb
      09-04 14:11:57.679: I/System.out(16814): MY DATABASE SIZE1
      09-04 14:11:57.684: I/System.out(16814): Loop Value:--0
      09-04 14:11:57.684: I/System.out(16814): Location Details:-----vjgt
      09-04 14:11:57.684: I/System.out(16814): Description Details:------gugf
      09-04 14:11:57.714: D/AndroidRuntime(16814): Shutting down VM
      09-04 14:11:57.714: W/dalvikvm(16814): threadid=1: thread exiting with uncaught exception (group=0x417422a0)

       09-04 14:34:57.834: E/AndroidRuntime(18547): FATAL EXCEPTION: main
       09-04 14:34:57.834: E/AndroidRuntime(18547): java.lang.NullPointerException
       09-04 14:34:57.834: E/AndroidRuntime(18547):     at com.cdotsys.adapters.CustomListViewAdapter.getView(CustomListViewAdapter.java:56)



      09-04 14:11:57.764: E/AndroidRuntime(16814): FATAL EXCEPTION: main
      09-04 14:11:57.764: E/AndroidRuntime(16814): java.lang.NullPointerException
      09-04 14:11:57.764: E/AndroidRuntime(16814):  at java.io.File.fixSlashes(File.java:185)
      09-04 14:11:57.764: E/AndroidRuntime(16814):  at java.io.File.<init>(File.java:134)

      09-04 14:11:57.764: E/AndroidRuntime(16814):  at android.widget.AbsListView.obtainView(AbsListView.java:2465)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.ListView.onMeasure(ListView.java:1162)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.View.measure(View.java:15504)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.View.measure(View.java:15504)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at  android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.View.measure(View.java:15504)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.View.measure(View.java:15504)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
      09-04 14:11:57.764: E/AndroidRuntime(16814):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.View.measure(View.java:15504)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.View.measure(View.java:15504)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
     09-04 14:11:57.764: E/AndroidRuntime(16814):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.os.Handler.handleCallback(Handler.java:615)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.os.Handler.dispatchMessage(Handler.java:92)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.os.Looper.loop(Looper.java:137)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at android.app.ActivityThread.main(ActivityThread.java:4921)
    09-04 14:11:57.764: E/AndroidRuntime(16814):    at java.lang.reflect.Method.invokeNative(Native Method)
   09-04 14:11:57.764: E/AndroidRuntime(16814):     at java.lang.reflect.Method.invoke(Method.java:511)
   09-04 14:11:57.764: E/AndroidRuntime(16814):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
   09-04 14:11:57.764: E/AndroidRuntime(16814):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   09-04 14:11:57.764: E/AndroidRuntime(16814):     at dalvik.system.NativeStart.main(Native Method)

ここでは、データベースに関して問題がないことがわかります。私が推測するビューのアップグレードに関していくつかの問題があります。誰か助けてください。お願いします!! メインスレッドで発生している Null Pointer Exception を取り除く方法を教えてください。

4

2 に答える 2

0
    please check this

      @Override
                public int getCount() {
                    if (arr == null )
                         return 0;
                    return arr.size();
                }


        @Override
        public Object getItem(int arg0) {
            if (arr == null)
                 return null;
             else 
                return arr.get(arg0);
        }

and check the null value in getView
于 2013-09-04T09:26:33.107 に答える