(Liste görünümü) findViewById (R.id.listView1) boş dönen

oy
0

Ben Veritabanı kullanarak ilk uygulamam oluşturmak için bu şablonu kullanmaya çalışıyorum ama bu noktada vurdu ediyorum. diğer çözümlerle görüldüğü gibi, setContentView (düzen) önce işlevini çağırarak veya temizleyin> Projesi'nin temiz çıktı alma gibi hiçbir Hatalar vardı.

aşağıdaki gibi Benim Etkinlik Java dosyasıdır

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  dbHelper = new CountriesDbAdapter(this);
  dbHelper.open();

  //Clean all data
  dbHelper.deleteAllCountries();
  //Add some data
  dbHelper.insertSomeCountries();

  //Generate ListView from SQLite Database
  displayListView(); /*Error Here*/ /*NULLreference Exception*/

 }

Hata Yukarıdaki kod son satırında atılır

Ana java devam

private void displayListView() {


  Cursor cursor = dbHelper.fetchAllCountries();

  // The desired columns to be bound
  String[] columns = new String[] {
    CountriesDbAdapter.KEY_CODE,
    CountriesDbAdapter.KEY_NAME,
    CountriesDbAdapter.KEY_CONTINENT,
    CountriesDbAdapter.KEY_REGION
  };

  // the XML defined views which the data will be bound to
  int[] to = new int[] { 
    R.id.code,
    R.id.name,
    R.id.continent,
    R.id.region,
  };

  // create the adapter using the cursor pointing to the desired data 
  //as well as the layout information
  dataAdapter = new SimpleCursorAdapter(
    this, R.layout.country_info, 
    cursor, 
    columns, 
    to,
    0);

  ListView listView = (ListView) findViewById(R.id.listView1);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter); /*NULLreference Exception*/

şöyle main.xml olduğunu

<?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=vertical>

 <TextView android:layout_width=fill_parent
  android:layout_height=wrap_content android:padding=10dp
  android:[email protected]/some_text android:textSize=20sp />

 <EditText android:[email protected]+id/myFilter android:layout_width=match_parent
  android:layout_height=wrap_content android:ems=10 
  android:[email protected]/some_hint>
  <requestFocus />
 </EditText>

 <ListView android:[email protected]+id/listView1 android:layout_width=fill_parent
  android:layout_height=fill_parent />

</LinearLayout>

şöyle Ve Country_info.xml olduğunu

<?xml version=1.0 encoding=utf-8?>
<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
    android:layout_width=fill_parent
    android:layout_height=wrap_content
    android:orientation=vertical
    android:padding=6dip >

    <TextView
        android:[email protected]+id/textView1
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:layout_alignParentLeft=true
        android:layout_alignParentTop=true
        android:text=Code: 
        android:textAppearance=?android:attr/textAppearanceMedium />

    <TextView
        android:[email protected]+id/textView2
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView1
        android:[email protected]+id/textView1
        android:text=Name: 
        android:textAppearance=?android:attr/textAppearanceMedium />

    <TextView
        android:[email protected]+id/textView3
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView2
        android:[email protected]+id/textView2
        android:text=Continent: 
        android:textAppearance=?android:attr/textAppearanceMedium />

    <TextView
        android:[email protected]+id/textView4
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView3
        android:[email protected]+id/textView3
        android:text=Region: 
        android:textAppearance=?android:attr/textAppearanceMedium />

    <TextView
        android:[email protected]+id/continent
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView3
        android:[email protected]+id/textView3
        android:[email protected]+id/textView3
        android:text=TextView />

    <TextView
        android:[email protected]+id/region
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView4
        android:[email protected]+id/textView4
        android:[email protected]+id/continent
        android:text=TextView />

    <TextView
        android:[email protected]+id/name
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView3
        android:[email protected]+id/textView3
        android:text=TextView />

    <TextView
        android:[email protected]+id/code
        android:layout_width=wrap_content
        android:layout_height=wrap_content
        android:[email protected]+id/textView2
        android:[email protected]+id/name
        android:text=TextView />

</RelativeLayout>

Hemen hemen, burada ben sorunu çözüldü searchin oldum Hiçbir çözüm sıkışmış :(

DÜZENLE:

Adaptör Sınıf

public class CountriesDbAdapter {

 public static final String KEY_ROWID = _id;
 public static final String KEY_CODE = code;
 public static final String KEY_NAME = name;
 public static final String KEY_CONTINENT = continent;
 public static final String KEY_REGION = region;

 private static final String TAG = CountriesDbAdapter;
 private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

 private static final String DATABASE_NAME = World;
 private static final String SQLITE_TABLE = Country;
 private static final int DATABASE_VERSION = 1;

 private final Context mCtx;

 private static final String DATABASE_CREATE =
  CREATE TABLE if not exists  + SQLITE_TABLE +  ( +
  KEY_ROWID +  integer PRIMARY KEY autoincrement, +
  KEY_CODE + , +
  KEY_NAME + , +
  KEY_CONTINENT + , +
  KEY_REGION + , +
   UNIQUE ( + KEY_CODE +));;

 private static class DatabaseHelper extends SQLiteOpenHelper {

  DatabaseHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }


  @Override
  public void onCreate(SQLiteDatabase db) {
   Log.w(TAG, DATABASE_CREATE);
   db.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   Log.w(TAG, Upgrading database from version  + oldVersion +  to 
     + newVersion + , which will destroy all old data);
   db.execSQL(DROP TABLE IF EXISTS  + SQLITE_TABLE);
   onCreate(db);
  }
 }

 public CountriesDbAdapter(Context ctx) {
  this.mCtx = ctx;
 }

 public CountriesDbAdapter open() throws SQLException {
  mDbHelper = new DatabaseHelper(mCtx);
  mDb = mDbHelper.getWritableDatabase();
  return this;
 }

 public void close() {
  if (mDbHelper != null) {
   mDbHelper.close();
  }
 }

 public long createCountry(String code, String name, 
   String continent, String region) {

  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_CODE, code);
  initialValues.put(KEY_NAME, name);
  initialValues.put(KEY_CONTINENT, continent);
  initialValues.put(KEY_REGION, region);

  return mDb.insert(SQLITE_TABLE, null, initialValues);
 }

 public boolean deleteAllCountries() {

  int doneDelete = 0;
  doneDelete = mDb.delete(SQLITE_TABLE, null , null);
  Log.w(TAG, Integer.toString(doneDelete));
  return doneDelete > 0;

 }

 public Cursor fetchCountriesByName(String inputText) throws SQLException {
  Log.w(TAG, inputText);
  Cursor mCursor = null;
  if (inputText == null  ||  inputText.length () == 0)  {
   mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
     null, null, null, null, null);

  }
  else {
   mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
     KEY_NAME +  like '% + inputText + %', null,
     null, null, null, null);
  }
  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;

 }

 public Cursor fetchAllCountries() {

  Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
    KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION}, 
    null, null, null, null, null);

  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;
 }

 public void insertSomeCountries() {

  createCountry(AFG,Afghanistan,Asia,Southern and Central Asia);
  createCountry(ALB,Albania,Europe,Southern Europe);
  createCountry(DZA,Algeria,Africa,Northern Africa);
  createCountry(ASM,American Samoa,Oceania,Polynesia);
  createCountry(AND,Andorra,Europe,Southern Europe);
  createCountry(AGO,Angola,Africa,Central Africa);
  createCountry(AIA,Anguilla,North America,Caribbean);

 }

}

Tüm bağımlılıklar ithal edilmişti ...

DÜZENLEME 2:

Hata Günlüğü:

04-02 15:20:02.400: E/AndroidRuntime(1038): FATAL EXCEPTION: main
04-02 15:20:02.400: E/AndroidRuntime(1038): Process: com.somesample, PID: 1038
04-02 15:20:02.400: E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.annahockeyleague.ourFamily/com.somesample.main}: java.lang.NullPointerException
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.os.Looper.loop(Looper.java:136)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at java.lang.reflect.Method.invoke(Method.java:515)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at dalvik.system.NativeStart.main(Native Method)
**04-02 15:20:02.400: E/AndroidRuntime(1038): Caused by: java.lang.NullPointerException
04-02 15:20:02.400: E/AndroidRuntime(1038):     at com.somesample.main.displayListView(main.java:71)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at com.somesample.main.onCreate(main.java:38)**
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.Activity.performCreate(Activity.java:5231)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 15:20:02.400: E/AndroidRuntime(1038):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-02 15:20:02.400: E/AndroidRuntime(1038):     ... 11 more
Oluştur 02/04/2014 saat 17:50
kullanıcı
Diğer dillerde...                            


1 cevaplar

oy
0

Sorun bazen Eclipse IDE üzerinden bir etkinlik oluşturmak ve çerçeve xml üzerinde çalışıyor ve etkinlik XML referans verebilirdin olmasıdır.

Sen çerçevelerle çalışıyorsanız o zaman bu kod parçası ile aktiviteye geri bağlantı emin olun

public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_some_xml, container, false);
            return rootView;
        }
    }

etkinlik sınıfın sonunda

Cevap 08/04/2014 saat 04:37
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more