Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库填充listview错误_Java_Android_Listview - Fatal编程技术网

Java 从数据库填充listview错误

Java 从数据库填充listview错误,java,android,listview,Java,Android,Listview,嘿,伙计们,我遇到了一个错误(NullPointerException),需要你们的帮助。无论我怎么努力,我都找不到我的错误 我的一般性讨论活动甚至无法打开。提前感谢。我的部分代码如下: 一般讨论 public class GeneralDiscussion extends ListActivity { SQLiteDatabase sqLiteDatabase; SQLiteHandler sqLiteHandler; Cursor cursor; ListView l; private

嘿,伙计们,我遇到了一个错误(
NullPointerException
),需要你们的帮助。无论我怎么努力,我都找不到我的错误

我的一般性讨论活动甚至无法打开。提前感谢。我的部分代码如下:

一般讨论

public class GeneralDiscussion extends ListActivity {


SQLiteDatabase sqLiteDatabase;
SQLiteHandler sqLiteHandler;
Cursor cursor;
ListView l;
private static final String TAG = GeneralDiscussion.class.getSimpleName();
private Button btnposting;
private Button btnLinkToLogin;
private EditText inputPost;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
private TextView txtName;
private TextView txtPost;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_general_discussion);


    ArrayList<HashMap<String, String>> emaillist = db.getMessage();
    if (emaillist.size() != 0) {
        Toast.makeText(getApplicationContext(), "this" + emaillist.get(1), Toast.LENGTH_SHORT).show();

        ListAdapter adapter = new SimpleAdapter(GeneralDiscussion.this, emaillist,
                R.layout.raw_layout,
                new String[]{"user_posted", "post", "posted_at", "post_id"}, new int[]{
                R.id.text_user_name, R.id.text_user_post, R.id.text_user_date, R.id.text_user_number});
        setListAdapter(adapter);
    }

    txtName = (TextView) findViewById(R.id.name);
    inputPost = (EditText) findViewById(R.id.post);
    btnposting = (Button) findViewById(R.id.btnposting);

    // Progress dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    // Session manager
    session = new SessionManager(getApplicationContext());

    // SQLite database handler
    db = new SQLiteHandler(getApplicationContext())

    HashMap<String, String> user = db.getUserDetails();
    String name = user.get("name");
    txtName.setText(name);

    // Posting Button Click event
    btnposting.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String post = inputPost.getText().toString().trim();
            String user_posted = txtName.getText().toString().trim();

            if (!post.isEmpty()) {
                postThreads(post, user_posted)

            } else {
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
}
private void postThreads(final String post,final String user_posted) {
    // Tag used to cancel the request
    String tag_string_req = "req_posting";
    pDialog.setMessage("Posting ...");
    showDialog();
    StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_POSTING, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Posting Response: " + response.toString());
            hideDialog();
            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    // User successfully stored in MySQL
                    // Now store the user in sqlite

                    JSONObject posting = jObj.getJSONObject("posting");
                    String post = posting.getString("post");
                    String user_posted = posting.getString("user_posted");
                    String posted_at = posting
                            .getString("posted_at");

                    // Inserting row in users table
                    db.addPosting(user_posted,post,posted_at);

                    Toast.makeText(getApplicationContext(), "User successfully posted!", Toast.LENGTH_LONG).show();

               } else {

                    // Error occurred in registration. Get the error
                    // message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
           }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Posting Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("post", post);
            params.put("user_posted", user_posted);
          return params;
        }
    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}
private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
         <?xml version="1.0" encoding="utf-8"?> <LinearLayout   android:id="@+id/main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">


    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="286dp">

        <ListView
            android:id="@android:id/list"
            android:layout_height="251dp"
            android:layout_width="fill_parent">
        </ListView>
    </LinearLayout>




<Button
        android:id="@+id/btnposting"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="#0000"
        android:text="Write..."
        android:textColorHighlight="#000000" />

    <EditText
        android:id="@+id/name"
        android:layout_width="203dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="#ffffff"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/post"
        android:layout_width="234dp"
        android:layout_height="87dp"
        android:layout_marginBottom="10dp"
        android:padding="10dp"
        android:singleLine="true" />

</LinearLayout>

onCreate()
方法的这一行,您将获得NPE-

ArrayList<HashMap<String, String>> emaillist = db.getMessage();//db is not instantiated yet. 
ArrayList emaillist=db.getMessage()//数据库尚未实例化。
您正在访问尚未实例化的数据库上的getMessage()方法。
在对其调用方法之前,需要对其进行实例化

在onCreate中初始化db谢谢我这么做了,但现在我得到了java.lang.IndexOutOfBoundsException:索引1无效,大小为1。删除此行Toast.makeText(getApplicationContext(),“this”+emaillist.get(1),Toast.LENGTH\u SHORT).show();谢谢,它很有魅力!:)为什么不使用
SimpleCursorAdapter
而不是
simpledapter
   <TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/text_user_post"
android:text="Post"
android:gravity="center"



    />
<TextView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/text_user_name"
    android:text="Name"
    android:gravity="center"


    />
<TextView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/text_user_number"
    android:text="Number"
    android:gravity="center"


    />

<TextView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/text_user_date"
    android:text="Date"  
   android:gravity="center"
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tonia/com.tonia.activity.GeneralDiscussion}: java.lang.NullPointerException
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                         at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                         at android.os.Handler.dispatchMessage(Handler.java:99)
                                                         at android.os.Looper.loop(Looper.java:137)
                                                         at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:511)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
                                                         at com.tonia.activity.GeneralDiscussion.onCreate(GeneralDiscussion.java:86)
                                                         at android.app.Activity.performCreate(Activity.java:4466)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
                                                         at android.app.ActivityThread.access$600(ActivityThread.java:123) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                         at android.os.Looper.loop(Looper.java:137) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:4424) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:511) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
                                                         at dalvik.system.NativeStart.main(Native Method) 
ArrayList<HashMap<String, String>> emaillist = db.getMessage();//db is not instantiated yet.