Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我试图从数据库中创建和检索数据,但出现“空指针异常”_Android_Sqlite_Android Sqlite_Sqliteopenhelper - Fatal编程技术网

Android 我试图从数据库中创建和检索数据,但出现“空指针异常”

Android 我试图从数据库中创建和检索数据,但出现“空指针异常”,android,sqlite,android-sqlite,sqliteopenhelper,Android,Sqlite,Android Sqlite,Sqliteopenhelper,我是android开发新手,我正在学习使用SQLite数据库,当我创建一个数据库并从中检索数据时,会出现这个错误。 如果您需要查看其他代码,请务必告诉我 以下是我的主要活动: public static final String LOGTAG = "EMPLOYEES"; public static final String Name = "name"; public static final String Pic = "pic"; public static final String Pos

我是android开发新手,我正在学习使用SQLite数据库,当我创建一个数据库并从中检索数据时,会出现这个错误。 如果您需要查看其他代码,请务必告诉我

以下是我的主要活动:

public static final String LOGTAG = "EMPLOYEES";

public static final String Name = "name";
public static final String Pic = "pic";
public static final String Position = "position";
public static final String Qualification = "qualification";
public static final String Expertise = "expertise";
public static final String Contact = "contact";


public static final int DETAIL_REQUEST_CODE = 1001;
protected List<TeamDetails> data;

private SharedPreferences settings;
private SharedPreferences.OnSharedPreferenceChangeListener listener;

EmployeeDataSource datasource;

private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    datasource = new EmployeeDataSource(this);
    datasource.open();

    List<TeamDetails> teamDetails = datasource.findAll();
    if (teamDetails.size() == 0) {
        createData();
        teamDetails = datasource.findAll();
    }


    ArrayAdapter<TeamDetails> courseArrayAdapter =
            new nameArrayAdapter(this, 0, data);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(courseArrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TeamDetails teamDetails = data.get(position);
            displayDetail(teamDetails);
        }
    });

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
protected void onResume() {
    super.onResume();
    datasource.open();
}

@Override
protected void onPause() {
    super.onPause();
    datasource.close();
}

private void createData() {

    TeamDetails teamDetails = new TeamDetails();
    teamDetails.setName("M Jaleed");
    teamDetails.setPosition("Team Lead");
    teamDetails.setQualities("Masters");
    teamDetails.setExpertise("Android Development");
    teamDetails.setContact(03331234567);
    teamDetails.setImage("");
    teamDetails = datasource.create(teamDetails);
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId());

    teamDetails = new TeamDetails();
    teamDetails.setName("Sajawal Nawaz");
    teamDetails.setPosition("Internee");
    teamDetails.setQualities("Bachelors");
    teamDetails.setExpertise("Android Development");
    teamDetails.setContact(03331234567);
    teamDetails.setImage("");
    teamDetails = datasource.create(teamDetails);
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId());

    teamDetails = new TeamDetails();
    teamDetails.setName("Waqas Khan");
    teamDetails.setPosition("Internee");
    teamDetails.setQualities("Bachelors");
    teamDetails.setExpertise("Android Development");
    teamDetails.setContact(03331234567);
    teamDetails.setImage("");
    teamDetails = datasource.create(teamDetails);
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId());

    teamDetails = new TeamDetails();
    teamDetails.setName("Waqas Khan Swat");
    teamDetails.setPosition("Internee");
    teamDetails.setQualities("Bachelors");
    teamDetails.setExpertise("Android Development");
    teamDetails.setContact(03331234567);
    teamDetails.setImage("");
    teamDetails = datasource.create(teamDetails);
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId());

    teamDetails = new TeamDetails();
    teamDetails.setName("Arslan Shah");
    teamDetails.setPosition("Internee");
    teamDetails.setQualities("Bachelors");
    teamDetails.setExpertise("Android Development");
    teamDetails.setContact(03331234567);
    teamDetails.setImage("");
    teamDetails = datasource.create(teamDetails);
    Log.i(LOGTAG, "Details has been added" + teamDetails.getId());


}


private void displayDetail(TeamDetails teamDetails) {
    Intent intent = new Intent(this, DetailActivity.class);
    intent.putExtra(Name, teamDetails.getName());
    intent.putExtra(Pic, teamDetails.getImage());
    intent.putExtra(Position, teamDetails.getPosition());
    intent.putExtra(Qualification, teamDetails.getQualities());
    intent.putExtra(Expertise, teamDetails.getExpertise());
    intent.putExtra(Contact, teamDetails.getContact());
    startActivityForResult(intent, DETAIL_REQUEST_CODE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onStart() {
    super.onStart();

    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW,
            "Main Page", 
            Uri.parse("http://host/path"),

            Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, 
            "Main Page", 
    Uri.parse("http://host/path"),

            Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}


class nameArrayAdapter extends ArrayAdapter<TeamDetails> {

    Context context;
    List<TeamDetails> objects;

    public nameArrayAdapter(Context context, int resource, List<TeamDetails> objects) {
        super(context, resource, objects);

        this.context = context;
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TeamDetails teamDetails = objects.get(position);

        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.list, null);

        TextView tv = (TextView) view.findViewById(R.id.name);
        tv.setText(teamDetails.getName());

        ImageView iv = (ImageView) view.findViewById(R.id.empPic);
        int res = context.getResources().getIdentifier(teamDetails.getImage(), "drawable", context.getPackageName()
        );
        iv.setImageResource(res);

        return view;
    }

}
这是我的数据源类

    package DgApps3.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.sj.dgapps3.TeamDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SJ on 4/14/2016.
 */
public class EmployeeDataSource {

    public static final String LOGTAG = "EMPLOYEES";

    SQLiteOpenHelper dbhelper;
    SQLiteDatabase database;

    private static final String[] allColumns = {
            DgApps3DBOpenHelper.COLUMN_ID,
            DgApps3DBOpenHelper.COLUMN_NAME,
            DgApps3DBOpenHelper.COLUMN_POSITION,
            DgApps3DBOpenHelper.COLUMN_PIC,
            DgApps3DBOpenHelper.COLUMN_EXPERTISE,
            DgApps3DBOpenHelper.COLUMN_QUALIFICATION,
            DgApps3DBOpenHelper.COLUMN_CONTACT
    };

    public EmployeeDataSource(Context context) {
        dbhelper = new DgApps3DBOpenHelper(context);
        //database = dbhelper.getWritableDatabase();
    }

    public void open() {
        Log.i(LOGTAG, "Database opened");
        database = dbhelper.getWritableDatabase();
    }

    public void close() {
        Log.i(LOGTAG, "Database closed");
        dbhelper.close();
    }

    public TeamDetails create(TeamDetails teamDetails) {
        ContentValues values = new ContentValues();
        values.put(DgApps3DBOpenHelper.COLUMN_NAME, teamDetails.getName());
        values.put(DgApps3DBOpenHelper.COLUMN_POSITION, teamDetails.getPosition());
        values.put(DgApps3DBOpenHelper.COLUMN_PIC, teamDetails.getImage());
        values.put(DgApps3DBOpenHelper.COLUMN_QUALIFICATION, teamDetails.getQualities());
        values.put(DgApps3DBOpenHelper.COLUMN_EXPERTISE, teamDetails.getExpertise());
        values.put(DgApps3DBOpenHelper.COLUMN_CONTACT, teamDetails.getContact());
        long insertid = database.insert(DgApps3DBOpenHelper.TABLE_EMPLOYEES, null, values);
        teamDetails.setId(insertid);
        return teamDetails;
    }

    public List<TeamDetails> findAll() {
        List<TeamDetails> teamDetails = new ArrayList<TeamDetails>();

        Cursor cursor = database.query(DgApps3DBOpenHelper.TABLE_EMPLOYEES, allColumns,
                null, null, null, null, null);

        Log.i(LOGTAG, "Returned" + cursor.getCount() + " rows ");
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                TeamDetails teamDetail = new TeamDetails();
                teamDetail.setId(cursor.getLong(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_ID)));
                teamDetail.setName(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_NAME)));
                teamDetail.setPosition(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_POSITION)));
                teamDetail.setImage(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_PIC)));
                teamDetail.setQualities(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_QUALIFICATION)));
                teamDetail.setExpertise(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_EXPERTISE)));
                teamDetail.setContact(cursor.getDouble(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_CONTACT)));
                teamDetails.add(teamDetail);
            }

        }
        return teamDetails;
    }
}

如果将stacktrace读得足够近,就会看到在调用列表上的size方法时发生错误。您可能会说,您从不调用size方法。没错,但安卓确实

看看这个代码

protected List<TeamDetails> data;
你有没有初始化过这个列表?不,所以它是空的。那么,你认为这会怎样

ArrayAdapter<TeamDetails> courseArrayAdapter =
        new nameArrayAdapter(this, 0, data);

它不会,因此会出现错误。要修复它,请在适配器中使用适当的列表,或在将其放入适配器之前初始化该列表

list data=new list@Sushil List data=new ArrayList List is interface他没有给List分配内存|这就是空指针出现的原因。@Maheshwarligate我在另一个类中创建了列表,只是在这里使用它。@Sushil kumar你不能创建接口列表的对象是一个接口我已经初始化了它,但问题在于这一行代码listView.SetAdapterCoursaryAdapter;如果我对此进行注释,那么错误就消失了,我的列表也消失了。是的,您的问题就在那一行,因为适配器中的数据变量为null。你不会有错误告诉你,直到你运行那条线。如果您更改了代码,请编辑您的问题和错误以反映这些更改。现在已解决,但下一个问题是图像无法在列表中传递。不确定您的意思,但请针对新问题发布新问题
ArrayAdapter<TeamDetails> courseArrayAdapter =
        new nameArrayAdapter(this, 0, data);