我的应用程序在访问游标时崩溃

时间:2013-12-23 08:17:15

标签: android android-contentprovider android-xml android-cursor

我需要一些关于我的下方应用程序的帮助,当我按下应该使用游标(内容提供程序)检索数据的按钮时它会崩溃,我实际上将其缩小以使光标从现成的数组中获取数据代码

这是代码

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

}

XML文件

<?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>

我的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

所以我实际上编辑了onclick动作并使光标只指向预期的数组,但它仍然崩溃

@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]);}
                }
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

迭代游标存在问题,这是迭代游标的示例代码

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

cur.moveToNext();
}

答案 1 :(得分:0)

将onClick更改为以下

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

改变你的状况

!Names[i].equals(values)

比较字符串

时应使用equals方法