【问题标题】:Database connection working well in Android Studio VDM but not in phone数据库连接在 Android Studio VDM 中运行良好,但在手机中却不行
【发布时间】:2015-04-12 15:50:28
【问题描述】:

我正在制作一个列表视图,用于加载数据库中的名称。 CustomerID 用于访问名称的详细视图。 我在 Android Studio VDM 中得到输出没有任何问题。但是当我在手机(Android 4.3)上运行它时,出现以下错误。

java.lang.NullPointerException:尝试在 com.tut.app.GetAllCustomerListViewAdapter.getCount(GetAllCustomerListViewAdapter.java:38) 的空对象引用上调用虚拟方法“int org.json.JSONArray.length()”

我的主要活动:

public class MainActivity extends ActionBarActivity {

    private ListView GetAllCustomerListView;
    private JSONArray jsonArray;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.GetAllCustomerListView = (ListView) this.findViewById(R.id.GetAllCustomerListView);

        new GetAllCustomerTask().execute(new ApiConnector());

        this.GetAllCustomerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try
                {
                    // GEt the customer which was clicked
                    JSONObject customerClicked = jsonArray.getJSONObject(position);

                    // Send Customer ID
                    Intent showDetails = new Intent(getApplicationContext(),CustomerDetailsActivity.class);
                    showDetails.putExtra("CustomerID", customerClicked.getInt("id"));

                    startActivity(showDetails);

                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

    }



    public  void setListAdapter(JSONArray jsonArray)
    {
        this.jsonArray = jsonArray;
        this.GetAllCustomerListView.setAdapter(new GetAllCustomerListViewAdapter(jsonArray,this));
    }




    private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
    {
        @Override
        protected JSONArray doInBackground(ApiConnector... params) {

            // it is executed on Background thread

             return params[0].GetAllCustomers();
        }

        @Override
        protected void onPostExecute(JSONArray jsonArray) {

            setListAdapter(jsonArray);


        }
    }

}

请帮忙! :(

【问题讨论】:

    标签: android json android-studio


    【解决方案1】:

    我发现出了什么问题! :D 我的笔记本电脑已连接到本地主机。因此,它在运行应用程序时没有问题。但只有当我在服务器中托管 php 文件和数据库时,该应用程序才能在手机中完美运行!希望这对某人有帮助!

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 2018-04-10
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多