8

我正在创建一个具有首选项活动的应用程序,其中 listpreference 条目有问题地分配。此应用程序在我的模拟器上运行良好,但是当我在移动设备中测试我的应用程序时,它显示如下错误

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2033)

当我在手机中单击该列表首选项时会出现此问题。这是我的代码

偏好.xml

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
            android:title="@string/settings">            
        <ListPreference
                android:key="@string/resolution"
                android:title="@string/imageres"
                android:summary="@string/summaryres"
                android:dialogTitle="@string/resFormat" 
                />
    </PreferenceCategory>
</PreferenceScreen>

设置.java

package com.example;

import android.database.Cursor;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {
    private SQLiteoperations SQLiteAdapter;
    DTO get;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        get = (DTO) getIntent().getExtras().get("obj");
        SQLiteAdapter = new SQLiteoperations(this);
        SQLiteAdapter.openToWrite();
        addPreferencesFromResource(R.xml.preferences);
        ListPreference pref = (ListPreference) findPreference("resolution");
        pref.setEntries(getEntries());
        pref.setEntryValues(getEntries());
        System.out.println("after set entries");
        System.out.println(get.getDefaultvalues());
        pref.setDefaultValue(get.getDefaultvalues());

    }
    private CharSequence[] getEntries() {
        Cursor cursor = SQLiteAdapter.queueAll();
        int num = cursor.getCount();
        String[] size = new String[num];
        System.out.println(cursor.getCount());
        if (cursor.moveToFirst()){
            int i=0;
               do{
                   size[i]=new String(cursor.getString(1)+"X"+cursor.getString(2));
                   System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
               }while(cursor.moveToNext());
            }
            cursor.close();

        return size;
    }

}

camerademo.java

package com.example;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class CameraDemo extends Activity {
    private final int DIALOG_LICENSE=5;
    private final int MENU_SETTINGS=2;
    private SharedPreferences settings;
    private static final String TAG = "CameraDemo";
    Camera camera;
    Preview preview;
    Button buttonClick,buttonSave,buttonBack;
    byte[] image;
    private static SQLiteoperations placeData;
    final DTO set = new DTO();
    String defsize;
    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        placeData = new SQLiteoperations(this);
        placeData.openToWrite();

        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);
        buttonSave = (Button) findViewById(R.id.Save);
        buttonBack = (Button) findViewById(R.id.back);
        buttonBack.setVisibility(View.INVISIBLE);
        buttonSave.setVisibility(View.INVISIBLE);
        buttonClick = (Button) findViewById(R.id.buttonClick);
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences licensPref = getSharedPreferences("PREFS", 0);
        boolean licenseShown = licensPref.getBoolean("LicenseShown", false);
        if(!licenseShown)
            showDialog(DIALOG_LICENSE);

        buttonClick.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                preview.camera.takePicture(shutterCallback, rawCallback,
                        jpegCallback);
                buttonClick.setVisibility(View.INVISIBLE);
                buttonSave.setVisibility(View.VISIBLE);
                buttonBack.setVisibility(View.VISIBLE);
            }
        });
        buttonBack.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                back();
            }
        });
        buttonSave.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                save();
                }
        });

        Log.d(TAG, "onCreate'd");
    }

    ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    // Handles data for raw picture 
    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };

    // Handles data for jpeg picture 
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            image = data;
        }
    };

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {     
        super.onPrepareDialog(id, dialog);
        }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id){
        case DIALOG_LICENSE:
            return new AlertDialog.Builder(this)
            .setTitle(R.string.welcome)
            .setMessage(R.string.welcome_text)
            .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    settings = getSharedPreferences("PREFS", 0);
                    Parameters parameters = preview.camera.getParameters();
                    Size defaultsize = parameters.getPictureSize();
                    defsize = (new String(defaultsize.width+"X"+defaultsize.height));
                    List<Camera.Size> size = parameters.getSupportedPictureSizes();
                    int num = size.size();
                    String[] sizeArray = new String[num];
                    int i=0;
                    Iterator<Size> si = size.iterator();
                    while (si.hasNext()) {
                        Camera.Size sizeObj = (Size) si.next();
                        sizeArray[i]=new String(sizeObj.width+ "X" +sizeObj.height);
                        Log.e("size in licence dialog", sizeObj.height+"x"+sizeObj.width);
                        i++;
                        placeData.insert(String.valueOf(sizeObj.width),String.valueOf(sizeObj.height));
                    }
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("LicenseShown", true);
                    editor.commit();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    finish();
                }              
            })
            .setCancelable(false)
            .create();
        }
        return null;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem mi;
        mi = menu.add(0, MENU_SETTINGS, 0, R.string.settings);
        mi.setIcon(android.R.drawable.ic_menu_preferences);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_SETTINGS:
            set.setDefaultvalues(defsize);
            Intent intent = new Intent(getApplicationContext(), Settings.class);
            intent.putExtra("obj", set);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
public void back(){
    preview.camera.startPreview();
    buttonBack.setVisibility(View.INVISIBLE);
    buttonSave.setVisibility(View.INVISIBLE);
    buttonClick.setVisibility(View.VISIBLE);
}
@Override
public void onDestroy(){
    super.onDestroy();
    if(preview.camera != null){
        preview.camera.stopPreview();
        preview.camera = null;
    }
}
public void save(){
           back();
            }
}

有人请告诉我哪里错了吗?

4

1 回答 1

11

有人请告诉我哪里错了吗?

您在代码中做错的事情是您在方法中初始化CharSequence数组的getEntries方式。你的代码:

if (cursor.moveToFirst()){
    int i=0;
    do{
       size[i]=new String(cursor.getString(1)+"X"+cursor.getString(2));
       System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
    }while(cursor.moveToNext());
}

size你用 的大小初始化数组,Cursor然后进入上面的 while 循环。问题是你的 while 循环只设置了值,i = 0因为你没有增加i' 在那个循环中的值。出于这个原因,您的size数组将只有位置0null所有其他位置的值。这将使ArrayAdapter投掷NullPointerException

正确的循环是:

if (cursor.moveToFirst()){
    int i=0;
    do{
       size[i]=new String(cursor.getString(1)+"X"+cursor.getString(2));
       System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
       i++;
    }while(cursor.moveToNext());
}

完整的getEntries方法是:

private CharSequence[] getEntries() {
    Cursor cursor = SQLiteAdapter.queueAll();
    int num = cursor.getCount();        
    String[] size = new String[num];
    System.out.println(cursor.getCount());
    if (cursor.moveToFirst()) {         
        int i = 0;
        do {
            size[i] = new String(cursor.getString(1) + "X"
                    + cursor.getString(2));             
            i++;
        } while (cursor.moveToNext());
    }
    cursor.close();
    return size;
}
于 2012-10-15T06:17:08.293 回答