更新 2
我基本上有 3 个类。对于我的主视图中的自定义列表视图。打开主活动时出现空指针异常并且列表没有出现的问题。首先查看我的主 xml 文件。
<?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>
正如您在这里看到的那样,xml 有两个按钮,一个图像视图和一个列表视图。现在列表从我得到两件事的主要活动中更新。1.)小文本 2.)大文本
现在我在我的主要活动 onCreate 方法中从本地数据库获取这两个东西。数据正在到来,正如我在 mylogcat 中看到的那样。所以数据库没有问题。所以这里是我的主要活动的 onCreate 方法。
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();
}
现在有两个 Other 类。一个是 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)
在这里你可以看到数据库没有问题。我猜升级视图有一些问题。请有人帮助我。拜托!!现在我怎样才能摆脱在我的主线程中发生的空指针异常。请帮助我。这让我很困扰很长时间:(