我无法找到为什么NPE会在这里发生。花了很多时间试图解决这个问题。使用日志我发现崩溃的位置(Records.class ...向下滚动)。如果需要,我也会给每个代码文件。我发现它不是上下文是空的。
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 我发现上下文不为空。使用日志我得到context=android.app.Application@4266f9e0 ..所以这不是上下文问题。数据也被填充。那么也许是R.layout.item中的问题?)
答案 0 :(得分:1)
终于找出了问题所在。感谢所有帮助过的人,问题出在lvrecords本身。它是空的。这就是原因。 在我的项目中,我有2个布局文件夹 - 用于纵向和横向模式。在这个应用程序中我从不使用肖像模式。一些肖像视图的xml文件只是空的...而且我感到羞耻,但这很有效,因为我限制了我的应用只使用横向模式。我实际上忘了这些文件是空的,比如records.xml。我认为Eclipse只会采用横向模式的xml文件,就像它一直在做的那样,但在这里它没有。因此,当我将listview添加到portrait文件夹中的records.xml时,它可以工作。 我道歉,因为除了我之外没有人能够知道这一点。我甚至忘了这件事。