Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:00:35+00:00 2026-06-13T00:00:35+00:00

I am creating a application with preference activity where the listpreference entries are problematically

  • 0

I am creating a application with preference activity where the listpreference entries are problematically assigned. This application works fine on my emulator but while i test my application in mobile it shows error like this

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)

This problem arises when i click that listpreference in my mobile. here this is my code

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

Anyone please tell where i am wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:00:36+00:00Added an answer on June 13, 2026 at 12:00 am

    Anyone please tell where i am wrong?

    The thing you did wrong in your code is the way you initialize the CharSequence array in the getEntries method. Your code:

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

    You initialize the size array with the size of the Cursor and then you go in to the while loop above. The problem is that your while loop only sets the value for i = 0 as you don’t increase i‘s value in that loop. For this reason your size array will have a value only for position 0 and null on all other positions. This will make the ArrayAdapter throw that NullPointerException.

    The correct loop would be:

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

    The full getEntries method would be:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a custom Preference to remember which activity a user wants to run
I'm creating a cool Home application in Android. As this is a Home app
I am creating one application on TextToSpeech. But when i am trying to run,it
I am creating application where in i have used preferences for changing font settings.
Im creating application in java using XML. XML- <?xml version=1.0 encoding=UTF-8?> <songlist id=slist> <song
I'm write installer for my web site. Installer creating application pool, virtual directory and
i creating an application with movie animation , i have a animation some group
am creating one application for student. I need to show digital clock with remaining
I am currently creating an application for compiling multiple java projects in one go
So I'm creating an application for my mobile programming class and I'm running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.