2

我找不到爲什麼NPE發生在這裏。花了很多時間試圖弄清楚這一點。使用日誌我發現崩潰(Records.class ...向下滾動)。如果需要的話,我還會給每個代碼文件。我發現它不是上下文。ListView SetAdapter空指針異常

Records.class

package com.wordpress.jimmaru.tutorial.SimpleGame; 

import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ListView; 
import android.widget.Toast; 

public class Records extends Activity{ 

    private Score_DS ds=new Score_DS(this); 
    Data_LV[] data; 
    String[] name,score; 
    ListView lvrecords; 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // если хотим, чтобы приложение постоянно имело портретную ориентацию 
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

      // если хотим, чтобы приложение было полноэкранным 
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

      // и без заголовка 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 

      setContentView(R.layout.records); 
      //setContentView(new GameView(this,null)); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     lvrecords=(ListView)findViewById(R.id.listView1); 


     Recfill(); 


     } 

    public void Recfill() 
    {ds.open(); 
    Log.d("Pidar","ds.open"); 
    name=ds.curspinner("Records","Player_Name"); 
    score=ds.curspinner("Records","Player_Score"); 
    Log.d("Pidar","curspinner"); 
    ds.close(); 
    Log.d("Pidar","ds.close"); 
    int i=0; 
    if (score.length>0){ 
    data=new Data_LV[score.length]; 
    Log.d("Pidar","data new"); 
    while (i<score.length) {Log.d("Pidar","cikl "+i); 
     data[i]=new Data_LV(); 
     Log.d("Pidar","data[i] new"); 
     data[i].id=Integer.toString(i+1); 
     data[i].name=name[i]; 
     data[i].score=score[i]; 

     i++; 
    } 
    Log.d("Pidar","nachalo adaptera"); 
    LV_Adapter adapter = new LV_Adapter(this, R.layout.item, data); 

    **lvrecords.setAdapter(adapter);** //This line doesn't execute 
    } 
    else 
    { Toast.makeText(this, "Записей нет", Toast.LENGTH_LONG).show();} 
    } 
} 

records.xml

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

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

    </ListView> 

</LinearLayout> 

item.xml

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

    <TextView 
     android:id="@+id/pl_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/pl_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/pl_score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="TextView" /> 

</LinearLayout> 

LV_Adapter

package com.wordpress.jimmaru.tutorial.SimpleGame; 

import java.util.List; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

class LV_Adapter extends ArrayAdapter<Data_LV> { 
    public LV_Adapter(Context context, int textViewResourceId, Data_LV[] objects) { 
     super(context, textViewResourceId, objects); 
    } 
    public LV_Adapter(Context context, int textViewResourceId, List<Data_LV> objects) { 
     super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getContext() 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.item, null); 
     TextView t1 = (TextView)convertView.findViewById(R.id.pl_id); 
     TextView t2 = (TextView)convertView.findViewById(R.id.pl_name); 
     TextView t3 = (TextView)convertView.findViewById(R.id.pl_score); 

     Data_LV d = getItem(position); 
     t1.setText(d.id); 
     t2.setText(d.name); 
     t3.setText(d.score); 


     return convertView; 
    } 

} 

Data_LV

package com.wordpress.jimmaru.tutorial.SimpleGame; 

public class Data_LV { 
String id; 
String name; 
String score; 
} 

記錄錯誤 enter image description here

UPD我發現這種情況下不爲空。使用日誌我得到了[email protected] ..所以這不是上下文問題。數據也被填寫。所以也許在R.layout.item的問題?)

+1

Records.java的第76行是什麼? – FMontano

+0

還有lvrecords.setAdapter(適配器); – anindis

+0

有時R文件不會正確生成。嘗試清理您的項目並重新運行。 –

回答

1

最後發現問題是什麼。感謝所有幫助過的人,這個問題本身就在lvrecords中。它是空的。這就是爲什麼。 在我的項目中,我有2個佈局文件夾 - 用於縱向和橫向模式。在這個應用程序,我從不使用肖像模式。而縱向視圖的一些xml文件只是空的...而且對我感到羞恥,但這個工作,因爲我限制了我的應用程序只使用橫向模式。我實際上忘記了這些文件是空的,比如records.xml。我認爲Eclipse只會採用橫向模式的xml文件,就像它一直在做的一樣,但是在這裏它沒有。所以當我將listview添加到portrait文件夾中的records.xml時,它就起作用了。 我道歉,除了我之外沒有人能知道這一點。即使我忘記了這一點。

+1

愚蠢的是,我犯了同樣的錯誤!我提到了兩個xml佈局文件,但我實際上只是在其中一個ListView中放置了一個ListView! – JanB

+0

謝謝你,現在我可以發表評論)謝謝 – anindis

+0

你可以通過點擊綠色的勾號來「接受」你自己的答案。 –