Question

I can't find out why NPE takes place here. Spent a lot of time trying to figure this out. Using logs I've found where comes the crash (Records.class...scroll down). Also I give every code-file if it's needed. I found out that it's NOT context is null.

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;
}

Log error enter image description here

UPD I Found out that context is not null. Using logs I got context=android.app.Application@4266f9e0.. So it's not context problem. Data is filled too. So maybe the problem in R.layout.item?)

Was it helpful?

Solution

Finally found out what was the problem. Thanks to all who helped, The problem was in lvrecords itself. It was null. And here's why. In my project I have 2 layout folders - for portrait and landscape mode. In this app I never use portrait mode. And some xml files of portrait view are just empty... And shame on me, but this worked, cause I limited my app for only using landscape mode. I actually forgot that these files are empty, like records.xml. And I thought that Eclipse would take only landscape mode xml files, like it did all the time, but here it didn't. So when I added listview to records.xml in portrait folder, it worked. I apologize cause no one except me could know this. Even I forgot about this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top