如问题所示,我需要显示数据库中的记录,每个孩子的不同记录。我, 孩子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 method
如何在不生成此异常的情况下访问onClick并根据需要显示记录? 我想我在某个地方会发生根本性的错误。如果有人能指出这个缺点我会很高兴。你看,这件事让我疯了。:/ / / p>
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
答案 0 :(得分:1)
应该如下。同时发布堆栈跟踪,以便我们可以进一步帮助您
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
}
});
}
}