2013-03-11 81 views
0

我正在創建一個應用程序,其中我創建了一個帶有兩個文本字段和一個複選框的列表視圖....當我單擊或選中複選框時,兩個文本都被添加到數據庫。但我的模擬器部隊停止了我的申請。請提前幫助... thanx。android模擬器中的java.lang.NullPointerException

這是一個名爲Database2Activity

package data.base; 
import android.app.ListActivity; 
import android.os.Bundle; 
public class Database2Activity extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    String[] str1 = new String[]{"Hari","Gopal","shayam","sita"}; 
    String[] str2 = new String[]{"123","2555","sksi","mahi"}; 
    setListAdapter(new Second(this, str1, str2)); 
} 
} 

這是我第二類我的第一個主要的Java類,它的名字Second.java

package data.base; 

import com.example.phone_no.DBhelper; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.EditText; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Second extends ArrayAdapter<String> { 
String string1[]; 

Context context; 
    String string[]; 
    TextView textView; 
    TextView textView1; 

    CheckBox checkBox; 
    EditText editText; 
    public Second(Context context, String[] objects, String[] Object1) 
    { 

     super(context,R.layout.main, objects); 

     this.context=context; 
     this.string=objects; 
     this.string1=Object1; 
     // this.imageView = Object3; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 

     LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view=inflater.inflate(R.layout.main,parent,false); 
      textView=(TextView)view.findViewById(R.id.text1); 


     textView1=(TextView)view.findViewById(R.id.text2); 
     // imageView = (ImageView)view.findViewById(R.id.text3); 
     textView.setText(string[position]); 
     textView1.setText(string1[position]); 

     checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 

       DBhelper DB = new DBhelper(context); 
       DB.open(); 
       DB.adddata("A", string[position], string1[position]); 
       DB.getAlldata(); 
       DB.close(); 


      } 
     }); 

     return view; //To change body of overridden methods use File | Settings | File Templates. 

    } 

} 

這是我的一個名爲DBhelper第三類,管理數據庫操作

package com.example.phone_no; 



import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBhelper { 
public static final String KEY_ID = "id"; 
public static final String KEY_NAME = "names"; 

public static final String KEY_PHONE = "phoneno"; 

private final Context ourContext; 
private static final String DATABASE_TABLE = "Contactinfo"; 
private static final int DATABASE_VERSION = 27; 
private static final String DATABASE_NAME = "contactdata.db"; 
private DbHelper ourHelper; 
private SQLiteDatabase ourDatabase; 
// end for location 
private static class DbHelper extends SQLiteOpenHelper { 
    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 

     /*db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID 
       + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT NULL , " 
       + KEY_PHONE + " INTEGER NULL);");*/ 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID 
       + " INTEGER , " + KEY_NAME + " TEXT NULL , " 
       + KEY_PHONE + " INTEGER NULL);"); 

     // string value 
     String y = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID 
       + " INTEGER PRIMARY KEY , " + KEY_NAME + " TEXT NULL , " 
       + KEY_PHONE + " INTEGER NULL);"; 

     System.out.println("query" + y); 
     Log.d("query", y); 






    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 

    } 

} 

public DBhelper(Context c) { 
    ourContext = c; 
} 
public DBhelper open() throws SQLException 
{ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    ourHelper.close(); 
} 
public long adddata(String id,String name,String number) 
{ 
    // TODO Auto-generated method stub 
    // add the custom Image Gallery Image Path to Data Base 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_ID, id); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_PHONE, number); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
} 

public void getAlldata() 
{ 
    Cursor details = null; 
    if (ourDatabase.isOpen() == false) 

     ourDatabase = ourHelper.getWritableDatabase(); 
    if (ourDatabase.isOpen()) 
    { 
     details = ourDatabase.query(DATABASE_TABLE, null, null, null, null, null, null); 
     for(details.moveToFirst();!details.isAfterLast();details.moveToNext()) 
     { 
      String a=details.getString(0); 
      String b=details.getString(1); 
      String c=details.getString(2); 
      System.out.println("id--"+a+"name"+b+"phoneno"+c); 
     } 



    } 


} 
public long delete_image(String id) 
    { 
    if (ourDatabase.isOpen() == false) 
     ourDatabase = ourHelper.getWritableDatabase(); 
     if (ourDatabase.isOpen()) 
     { 
      return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null); 
     } 
     return 0; 
    } 



} 

這是我的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<EditText 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/edit" 
/> 
<Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
    /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello" 
     android:layout_weight="1" 
     android:id="@+id/text1" 
     /> 
<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello" 
     android:layout_weight="1" 
     android:id="@+id/text2" 
     /> 
    <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@+id/check" 
      /> 



</LinearLayout> 

這是logcat的窗口按摩

03-12 02:04:21.028: D/AndroidRuntime(21905): Shutting down VM 
03-12 02:04:21.028: W/dalvikvm(21905): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
03-12 02:04:21.308: E/AndroidRuntime(21905): FATAL EXCEPTION: main 
03-12 02:04:21.308: E/AndroidRuntime(21905): java.lang.NullPointerException 
03-12 02:04:21.308: E/AndroidRuntime(21905): at data.base.Second.getView(Second.java:51) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.AbsListView.obtainView(AbsListView.java:1315) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.ListView.makeAndAddView(ListView.java:1727) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.ListView.fillDown(ListView.java:652) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.ListView.fillFromTop(ListView.java:709) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.ListView.layoutChildren(ListView.java:1580) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.AbsListView.onLayout(AbsListView.java:1147) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.view.View.layout(View.java:7035) 
03-12 02:04:21.308: E/AndroidRuntime(21905): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 

而且在過去的這是我的清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="data.base" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".Database2Activity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="Second"/> 
</application> 

</manifest> 
+0

您應該觀看Google I/O會談[TurboCharge您的用戶界面](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html)和[World或ListView](http:/ /www.google.com/events/io/2010/sessions/world-of-listview-android.html),它們將幫助您編寫_much_更快的適配器。 – Sam 2013-03-11 20:43:09

回答

1

onCreate刪除setContentView(R.layout.main); - 要覆蓋的ListActivity的默認視圖(其中包含android.R.id.list元素)與您正在使用的每個元素的佈局。

+0

同意,作者在_both_活動和適配器中使用'main.xml'。它看起來應該只在適配器中使用。 – Sam 2013-03-11 20:34:36

+0

ya我這樣做,當我再次運行我的代碼,然後它顯示我錯誤=「03-12 02:04:21.308:E/AndroidRuntime(21905):java.lang.NullPointerException 03-12 02:04:21.308:E/AndroidRuntime(21905):\t at data.base.Second.getView(Second.java:51) 「 – dev 2013-03-11 20:36:53

+0

@dev這是進步! :)在你的適配器改變'超(上下文,R.layout.main,對象);''超級(上下文,R.layout.main,R.id.text1,對象);' – Sam 2013-03-11 20:38:44

2
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 

你需要在你的ListActivity的佈局(main.xml)與ID使用一個ListView android:id="@android:id/list

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); // This layout must have a ListView 
+0

我該如何更改ListView的id。我是這個領域的新人,所以請解釋你的解決方案 – dev 2013-03-11 20:32:09

+0

嗯,你的活動和你的行佈局使用相同的佈局。活動的佈局應該有一個ListView,行佈局不應該... – Sam 2013-03-11 20:33:40

+0

它看起來像你想使用ListAcitivty的默認佈局,所以刪除'setContentView(R.layout.main);'。 _或創建一個帶有ListView的新XML文件,並在'setContentView(R.layout.listview_layout)'中使用它;' – Sam 2013-03-11 20:36:58