NullPointerException在android.widget.ArrayAdapter.createViewFromResource

我正在创build一个首选活动的应用程序,其中的列表首选项是有问题的分配。 此应用程序在我的模拟器上正常工作,但是当我在移动设备上testing我的应用程序时,它显示这样的错

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) 

当我点击我的手机中的列表首选项时出现此问题。 这里是我的代码

prference.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> 

settings.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(); } } 

任何人都请告诉我哪里错了?

Solutions Collecting From Web of "NullPointerException在android.widget.ArrayAdapter.createViewFromResource"

任何人都请告诉我哪里错了?

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

 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的大小初始化size数组,然后进入上面的while循环。 问题是你的while循环只设置了i = 0的值,因为你不会在那个循环中增加i的值。 出于这个原因,你的size数组只有位置0 null ,所有其他位置都是null 。 这将使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; }