正如问题所指示的,我需要显示数据库中的记录,每个孩子的不同记录。子1=记录1个数据。子2 =记录2个数据。某某。我编写了下面的显示代码:
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();
}
}
在文本中显示"no data",尽管我的数据库有数据,正如使用命令提示符adb shell确认的那样。所以我假设它没有访问onClick方法。现在,如果我取消onCreate方法中注释的行,它可以访问,但会导致NullPointerExceptionviewbutton。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
}
});
}
}