android-java.lang.NullPointerException:在可扩展列表视图中显示数据库中的记录

android-java.lang.NullPointerException:在可扩展列表视图中显示数据库中的记录,第1张

概述正如问题所指示的那样,我需要显示数据库中的记录,每个孩子都显示不同的记录.即,子1=记录1数据.子2=记录2data.so等.我想出了以下代码进行显示:packagecom.example.moolah;importandroid.app.ListActivity;importandroid.content.Intent;importandroid.database.Cursor

正如问题所指示的那样,我需要显示数据库中的记录,每个孩子都显示不同的记录.即,
子1 =记录1数据.子2 =记录2 data.so等.
我想出了以下代码进行显示:

package com.example.moolah;import androID.app.ListActivity;import androID.content.Intent;import androID.database.Cursor;import androID.os.Bundle;import androID.support.v4.Widget.SimpleCursorAdapter;import androID.vIEw.VIEw;import androID.Widget.button;public class displayActivity extends ListActivity {DBAdapter db=new DBAdapter(this);@OverrIDepublic voID onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_display_cursor);     button vIEwbutton=(button)findVIEwByID(R.ID.button2); //    vIEwbutton.setonClickListener(new buttonx());}public class buttonx extends displayActivity implements button.OnClickListener{ @SuppressWarnings("deprecation")  @OverrIDe    public voID onClick(VIEw v)    { button vIEwbutton=(button)v.findVIEwByID(R.ID.button2);        Object vIEwID=vIEwbutton.getTag();            int i=Integer.parseInt(vIEwID.toString());          MyCustomAdapter c=new MyCustomAdapter();            long ID=c.getChildID(0,i);        //displayCursor d;            //d.filldata();                         db.open();           // VIEw vIEw;           Cursor cursor = db.getRecord(ID);           startManagingCursor(cursor);            String[] columns = new String[] {DBAdapter.invest_type,DBAdapter.curr_per_share_price, DBAdapter.share_name,                    DBAdapter.no_of_shares,DBAdapter.share_IDentity,DBAdapter.purchase_price,                    DBAdapter.purchase_from,DBAdapter.purchase_date,DBAdapter.purchase_contact};            int[] to = new int[] { R.ID.investmenttype,R.ID.currpershareprice,R.ID.sharename,R.ID.shareno,R.ID.shareID, R.ID.purprice,                    R.ID.purfrom,R.ID.purdate,R.ID.purcon};            SimpleCursorAdapter mAdapter = new SimpleCursorAdapter( this, R.layout.row, cursor, columns, to);            this.setlistadapter(mAdapter);           db.close();    } }   

使用命令提示符adb shell确认,尽管我的数据库中有数据,但它以文本形式显示“无数据”.
因此,我认为它无权访问onClick方法.
现在,如果我取消注释onCreate方法中注释的行,它将获得访问权限,但会在以下位置导致NullPointerException
              vIEwbutton.setonClickListener(new buttonx()); // onCreate方法

如何在不生成此异常的情况下访问onClick并根据需要显示记录?
我认为我从某处根本上走错了.如果有人指出我的缺陷,我会非常感激.你看,这件事使我发疯.:/

logcat的:

     06-17 17:33:32.444: E/AndroIDRuntime(569):     FATAL EXCEPTION: main     06-17 17:33:32.444: E/AndroIDRuntime(569): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.example.moolah/com.example.moolah.displayActivity}: java.lang.NullPointerException   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1955)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.app.ActivityThread.access0(ActivityThread.java:122)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.os.Handler.dispatchMessage(Handler.java:99)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.os.Looper.loop(Looper.java:137)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at androID.app.ActivityThread.main(ActivityThread.java:4340)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at java.lang.reflect.Method.invokeNative(Native Method)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at java.lang.reflect.Method.invoke(Method.java:511)   06-17 17:33:32.444: E/AndroIDRuntime(569):   at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)    06-17 17:33:32.444: E/AndroIDRuntime(569):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)    06-17 17:33:32.444: E/AndroIDRuntime(569):  at dalvik.system.NativeStart.main(Native Method)            06-17 17:33:32.444: E/AndroIDRuntime(569): Caused by: java.lang.NullPointerException    06-17 17:33:32.444: E/AndroIDRuntime(569):  at com.example.moolah.displayActivity.onCreate(displayActivity.java:21)    06-17 17:33:32.444: E/AndroIDRuntime(569):  at androID.app.Activity.performCreate(Activity.java:4465)    06-17 17:33:32.444: E/AndroIDRuntime(569):  at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)    06-17 17:33:32.444: E/AndroIDRuntime(569):  at             androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1919)    06-17 17:33:32.444: E/AndroIDRuntime(569):  ... 11 more

解决方法:

它应该像下面这样.同时发布堆栈跟踪,以便我们进一步帮助您

public class displayActivity extends ListActivity {DBAdapter db;@OverrIDepublic voID onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_display_cursor);  db=new DBAdapter(this);  button vIEwbutton=(button)findVIEwByID(R.ID.button2);vIEwbutton.setonClickListener(new OnClickListener(){       @overrIDe       public voID onClick(VIEw v)       {             // do something       }   });}

编辑:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:paddingBottom="@dimen/activity_vertical_margin"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >  <ListVIEw androID:layout_height="wrap_content" androID:layout_above="@+ID/button1"androID:layout_wIDth="fill_parent" androID:ID="@+ID/lv" />  <button      androID:ID="@+ID/button1"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:layout_alignParentBottom="true"      androID:layout_centerHorizontal="true"      androID:text="button" /></relativeLayout>

MainActivity.java

public class MainActivity extends Activity {    ListVIEw lv;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        button b = (button) findVIEwByID(R.ID.button1);        lv= (ListVIEw) findVIEwByID(R.ID.lv);        b.setonClickListener(new OnClickListener()        {            @OverrIDe            public voID onClick(VIEw v) {                // Todo auto-generated method stub                // set your adapter to ListvIEw here            }        });    }}
总结

以上是内存溢出为你收集整理的android-java.lang.NullPointerException:在可扩展列表视图中显示数据库中的记录全部内容,希望文章能够帮你解决android-java.lang.NullPointerException:在可扩展列表视图中显示数据库中的记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存