正如问题所指示的那样,我需要显示数据库中的记录,每个孩子都显示不同的记录.即,
子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);
@Override
public 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.access$600(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;
@Override
public 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
}
});
}
}