0

i need some help on my below app, it crashes when i press on the button that is supposed to retrieve data using cursor (content provider) , i actually narrowed it down to make the cursor get data from a ready made array in the code

here is the code

public class DataManipulation extends Activity {

Button ImportButton, ViewButton;
EditText UserShow;
TextView EmailDisplay, UserDisplay, MobileDisplay, EmailTV, UserTV,
        MobileTV;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.datamanipulation);
    final ContentResolver ct = getContentResolver();
    String Path = "data/data/com.hossa.datamanipulation/files/userdata";
    final Uri uri = Uri.parse("content://" + Path);
    ImportButton = (Button) findViewById(R.id.ImportButton);
    ViewButton = (Button) findViewById(R.id.ViewButton);
    EmailDisplay = (TextView) findViewById(R.id.EmailDisplay);
    MobileDisplay = (TextView) findViewById(R.id.MobileDisplay);
    UserDisplay = (TextView) findViewById(R.id.UsernameDisplay);
    EmailTV = (TextView) findViewById(R.id.EmailTV2);
    UserTV = (TextView) findViewById(R.id.UserTV2);
    MobileTV = (TextView) findViewById(R.id.MobileTV2);
    UserShow = (EditText) findViewById(R.id.UserShownEdit);
    final String Names[] = { "hossam", "esraa", "islam", "7amada" };
    final String Emails[] = { "h@h.com", "e@e.com", "i@i.com", "7@7.com" };
    final String Mobiles[] = { "0101", "0202", "0303", "0404" };

    ImportButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            getApplicationContext();

            // TODO Auto-generated method stub
            SharedPreferences sp = getSharedPreferences("userdata",
                    Context.MODE_PRIVATE);

            String Username = sp.getString("Username", null);
            String Email = sp.getString("Email", null);
            String Mobile = sp.getString("Mobile", null);

            // what if there is no data from the user??//
            if (Username.equals(null) || Email.equals(null)
                    || Mobile.equals(null)) {
                Toast.makeText(getApplicationContext(),
                        "no data has been entered", Toast.LENGTH_SHORT)
                        .show();
            } else {
                UserTV.setText(Username);
                EmailTV.setText(Email);
                MobileTV.setText(Mobile);
            }
        }
    });

    ViewButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Cursor c = ct.query(uri, Names, null, null, null);
            if (c.getCount() > 0) {
                int i = 0;
                if (UserShow.equals(null)) {
                    Toast.makeText(getApplicationContext(),
                            "no data entered please enter data again",
                            Toast.LENGTH_SHORT).show();
                } else {
                    String values = UserShow.getText().toString();
                    while (Names[i] != values) {
                        i++;
                    }
                    UserTV.setText("" + Names[i]);
                    EmailTV.setText("" + Emails[i]);
                    MobileTV.setText("" + Mobiles[i]);
                }
            }
        }
    });

    // c=ct.delete(uri, where, selectionArgs);
    // c=ct.update(uri, values, where, selectionArgs);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

the XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/UsernameDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="34dp"
    android:layout_marginTop="30dp"
    android:text="@string/UsernameDisplay" />

<Button
    android:id="@+id/ImportButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/UsernameDisplay"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="130dp"
    android:text="@string/ImportButton" />

<TextView
    android:id="@+id/UserTV2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/UsernameDisplay"
    android:layout_below="@+id/UsernameDisplay"
    android:layout_marginTop="38dp"
    android:text="@string/UserSmallText"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/EmailTV2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/UserTV2"
    android:layout_centerHorizontal="true"
    android:text="@string/EmailSmallText"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/EmailDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ImportButton"
    android:layout_centerHorizontal="true"
    android:text="@string/EmailDisplay" />

<TextView
    android:id="@+id/MobileDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ImportButton"
    android:layout_alignParentRight="true"
    android:layout_marginRight="30dp"
    android:text="@string/MobileDisplay" />

<TextView
    android:id="@+id/MobileTV2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/EmailTV2"
    android:layout_alignBottom="@+id/EmailTV2"
    android:layout_alignRight="@+id/MobileDisplay"
    android:text="@string/MobileSmallText"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/ViewButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/ImportButton"
    android:layout_below="@+id/UserShownEdit"
    android:layout_marginTop="14dp"
    android:text="@string/ViewButton" />

<EditText
    android:id="@+id/UserShownEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ImportButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:ems="10"
    android:hint="@string/UserShownEdit"
    android:inputType="textPersonName" />

</RelativeLayout>

my logcat

    12-23 13:08:42.621: E/Trace(30142): error opening trace file: No such file or directory    (2)
    12-23 13:08:45.096: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:45.235: I/Choreographer(30142): Skipped 93 frames!  The application may be  doing too much work on its main thread.
    12-23 13:08:45.354: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:45.379: W/MMUMapper(30142): fail to register MVA, unsupported format(0x1)
    12-23 13:08:45.531: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:46.449: I/SurfaceTextureClient(30142): [0x516f4630] frames:17, duration:1.181000, fps:14.385322
    12-23 13:08:47.159: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:47.287: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:47.317: W/MMUMapper(30142): invalid operation for unregister MVA with VA(0x52970000) size(52224) f(0x1)
    12-23 13:08:47.353: W/MMUMapper(30142): invalid operation for unregister MVA with VA(0x52677000) size(614400) f(0x5)
    12-23 13:08:47.354: W/MMUMapper(30142): invalid operation for unregister MVA with VA(0x528d9000) size(614400) f(0x5)
    12-23 13:08:47.355: W/MMUMapper(30142): invalid operation for unregister MVA with VA(0x52baf000) size(614400) f(0x5)
    12-23 13:08:47.728: W/MMUMapper(30142): fail to register MVA, unsupported format(0x5)
    12-23 13:08:48.313: I/SurfaceTextureClient(30142): [0x52891fc8] frames:4, duration:1.090000, fps:3.666913
    12-23 13:08:48.408: W/dalvikvm(30142): threadid=1: thread exiting with uncaught exception (group=0x41216908)
    12-23 13:08:48.494: E/AndroidRuntime(30142): FATAL EXCEPTION: main
    12-23 13:08:48.494: E/AndroidRuntime(30142): java.lang.NullPointerException
    12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1118)
    12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.content.ContentResolver.query(ContentResolver.java:355)
    12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.content.ContentResolver.query(ContentResolver.java:314)
    12-23 13:08:48.494: E/AndroidRuntime(30142):    at com.hossa.datamanipulation.DataManipulation$2.onClick(DataManipulation.java:77)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.view.View.performClick(View.java:4093)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.view.View$PerformClick.run(View.java:17149)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.os.Handler.handleCallback(Handler.java:615)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.os.Looper.loop(Looper.java:153)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at android.app.ActivityThread.main(ActivityThread.java:5006)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at java.lang.reflect.Method.invoke(Method.java:511)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-23 13:08:48.494: E/AndroidRuntime(30142):    at dalvik.system.NativeStart.main(Native Method)
12-23 13:08:51.315: I/Process(30142): Sending signal. PID: 30142 SIG: 9
12-23 13:08:58.140: E/Trace(30530): error opening trace file: No such file or directory (2)
12-23 13:08:58.855: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:08:59.031: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:08:59.050: W/MMUMapper(30530): fail to register MVA, unsupported format(0x1)
12-23 13:08:59.143: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:09:00.019: I/SurfaceTextureClient(30530): [0x516f4630] frames:16, duration:1.031000, fps:15.504667
12-23 13:09:00.192: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:09:00.373: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:09:00.444: W/MMUMapper(30530): invalid operation for unregister MVA with VA(0x52677000) size(614400) f(0x5)
12-23 13:09:00.444: W/MMUMapper(30530): invalid operation for unregister MVA with VA(0x528d9000) size(614400) f(0x5)
12-23 13:09:00.445: W/MMUMapper(30530): invalid operation for unregister MVA with VA(0x52aa5000) size(614400) f(0x5)
12-23 13:09:00.754: W/MMUMapper(30530): fail to register MVA, unsupported format(0x5)
12-23 13:09:01.826: I/SurfaceTextureClient(30530): [0x528d2410] frames:4, duration:1.481000, fps:2.700157
12-23 13:09:02.044: W/MMUMapper(30530): invalid operation for unregister MVA with VA(0x52970000) size(52224) f(0x1)
12-23 13:09:02.863: I/SurfaceTextureClient(30530): [0x528d2410] frames:9, duration:1.113000, fps:8.085393
12-23 13:09:04.004: I/SurfaceTextureClient(30530): [0x528d2410] frames:3, duration:1.148000, fps:2.611027
12-23 13:09:05.153: I/SurfaceTextureClient(30530): [0x528d2410] frames:4, duration:1.156000, fps:3.460198
12-23 13:09:06.422: I/SurfaceTextureClient(30530): [0x528d2410] frames:3, duration:1.273000, fps:2.356522
12-23 13:09:07.532: I/SurfaceTextureClient(30530): [0x528d2410] frames:20, duration:1.110000, fps:18.017097
12-23 13:09:07.630: W/dalvikvm(30530): threadid=1: thread exiting with uncaught exception (group=0x41216908)
12-23 13:09:07.648: E/AndroidRuntime(30530): FATAL EXCEPTION: main
12-23 13:09:07.648: E/AndroidRuntime(30530): java.lang.NullPointerException
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1118)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.content.ContentResolver.query(ContentResolver.java:355)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.content.ContentResolver.query(ContentResolver.java:314)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at com.hossa.datamanipulation.DataManipulation$2.onClick(DataManipulation.java:77)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.view.View.performClick(View.java:4093)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.view.View$PerformClick.run(View.java:17149)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.os.Handler.handleCallback(Handler.java:615)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.os.Looper.loop(Looper.java:153)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at android.app.ActivityThread.main(ActivityThread.java:5006)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at java.lang.reflect.Method.invoke(Method.java:511)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-23 13:09:07.648: E/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-23 13:09:07.648: E/AndroidRuntime(30530):at dalvik.system.NativeStart.main(Native Method)
12-23 13:09:09.324: I/Process(30530): Sending signal. PID: 30530 SIG: 9

so i actually edited the onclick action and made the cursor point only to the array intended , but it still crashes

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Cursor c = ct.query(null,Names, null, null, null);
            if (c!=null&&c.getCount() > 0) {
                int i = 0;
                if (UserShow.getText().toString().equals(null) ||UserShow.getText().toString().equals("") ) {
                    Toast.makeText(getApplicationContext(),
                            "no data entered please enter data again",
                            Toast.LENGTH_SHORT).show();
                } else {
                    String values=UserShow.getText().toString();
                    do{
                        i++;
                        }while (!Names[i].equals(values));
                    if(Names[i].equals(values)){
                    UserTV.setText(Names[i]);
                    EmailTV.setText(Emails[i]);
                    MobileTV.setText(Mobiles[i]);}
                }
            }
        }
    });
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
Hossa The Coder
  • 77
  • 2
  • 10

2 Answers2

0

there is a problem in iterating cursor this is sample code to iterate cursor

cur.moveToFirst();
while (cur.isAfterLast() == false) 
{
//code to get data

cur.moveToNext();
}
Irshad Khan
  • 794
  • 6
  • 21
  • thx, i followed your method but it did not help and my logcat told me this "12-23 11:05:21.383: W/MMUMapper(18471): fail to register MVA, unsupported format(0x5)".......any advice? – Hossa The Coder Dec 23 '13 at 09:12
  • @HossaTheCoder check your database path(i.e. directory) it is usually stored in **/data/data/your.applications.package/databases** – Irshad Khan Dec 23 '13 at 10:00
0

Change onClick to following

 ViewButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Cursor c = ct.query(uri, Names, null, null, null);
        if (c!=null&&c.getCount() > 0) {
            int i = 0;
            if (UserShow.getText().toString().equals("")) {
                Toast.makeText(getApplicationContext(),
                        "no data entered please enter data again",
                        Toast.LENGTH_SHORT).show();
            } else {
                String values = UserShow.getText().toString();
                while (!Names[i].equals(values)) {
                    i++;
                }
                UserTV.setText("" + Names[i]);
                EmailTV.setText("" + Emails[i]);
                MobileTV.setText("" + Mobiles[i]);
            }
        }
    }
});

Change your condition

!Names[i].equals(values)

You should use equals method when comparing strings

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • thx that stopped the crashing but the view does not work and no output is made....any ideas why? – Hossa The Coder Dec 23 '13 at 09:53
  • what do you mean no output is made?? as in after click of the button or even before that – vipul mittal Dec 23 '13 at 10:05
  • after the button is pressed the cursor should have accessed the data and an output should be viewed on the TextViews....but nothing occurs – Hossa The Coder Dec 23 '13 at 10:07
  • did that but still it does not work....i actually changed the URI and put the Array string inside of it inorder to make the cursor go staright for the array as shown here " final String Names[] = { "hossam", "esraa", "islam", "7amada" };" , "final Uri uri = Uri.parse("content://" + Names);" " Cursor c = ct.query(uri,null, null, null, null);" but still no output – Hossa The Coder Dec 23 '13 at 10:33
  • i think cursor is returning null – vipul mittal Dec 23 '13 at 10:48
  • r you looking for all the phone contacts if yes try URI ContactsContract.Contacts.CONTENT_URI – vipul mittal Dec 23 '13 at 10:52
  • actually no i simply want the cursor to access the Names[] array and return values – Hossa The Coder Dec 23 '13 at 10:55
  • what is cursor achieving for you as in you have all the name and email id in array. So what is your cursor supposed to do?? – vipul mittal Dec 23 '13 at 11:39
  • the cursor is supposed to access this array and the operation return to me all the values of this contact in the array if he matches what the user entered in the EditText – Hossa The Coder Dec 23 '13 at 11:42
  • http://stackoverflow.com/questions/6330151/how-to-get-a-contacts-number-from-contact-name-in-android – vipul mittal Dec 23 '13 at 11:46
  • please check my code, i dont want to access the device's contacts, i have 3 arrays that i am working on and i want to access Names[] and manipulate it through query, thx\ – Hossa The Coder Dec 23 '13 at 11:53