我找不到爲什麼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;
}
記錄錯誤
UPD我發現這種情況下不爲空。使用日誌我得到了[email protected] ..所以這不是上下文問題。數據也被填寫。所以也許在R.layout.item的問題?)
Records.java的第76行是什麼? – FMontano
還有lvrecords.setAdapter(適配器); – anindis
有時R文件不會正確生成。嘗試清理您的項目並重新運行。 –