我正在为这个学校项目开发这个应用程序。应用程序背后的基础是使用volley从URL检索json数组,然后使用它来填充listview。听起来很简单,但由于我是应用程序开发新手,因此给了我最大的麻烦。因此,在经过无数次教程和无数错误之后,我设法解决了所有问题。但是现在当我运行应用程序时,我得到的只是一个空白屏幕。我的日志不会显示任何错误。
Log:
10-27 18:49:39.662 1997-1997/com.example.sahan.volley2 I/art: Not late-
enabling -Xcheck:jni (already on)
10-27 18:49:39.964 1997-2018/com.example.sahan.volley2
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-27 18:49:39.967 1997-1997/com.example.sahan.volley2 D/:
HostConnection::get() New Host Connection established 0xa5432140, tid
1997
10-27 18:49:39.979 1997-1997/com.example.sahan.volley2 D/Atlas:
Validating map...
10-27 18:49:40.156 1997-2018/com.example.sahan.volley2
I/OpenGLRenderer: Initialized EGL, version 1.4
10-27 18:49:40.210 1997-2018/com.example.sahan.volley2
D/OpenGLRenderer: Enabling debug mode 0
10-27 18:49:40.243 1997-2018/com.example.sahan.volley2 W/EGL_emulation:
eglSurfaceAttrib not implemented
10-27 18:49:40.243 1997-2018/com.example.sahan.volley2
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface
0xa5480b60, error=EGL_SUCCESS
10-27 18:49:40.696 1997-2018/com.example.sahan.volley2 W/EGL_emulation:
eglSurfaceAttrib not implemented
10-27 18:49:40.696 1997-2018/com.example.sahan.volley2
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface
0xa5480b80, error=EGL_SUCCESS
10-27 18:49:40.718 1997-1997/com.example.sahan.volley2 I/Choreographer:
Skipped 43 frames! The application may be doing too much work on its
main thread.
10-27 18:49:40.932 1997-1997/com.example.sahan.volley2 D/Volley: [1]
2.onErrorResponse: MainActivity
***** MainActivity.java ******
package com.example.sahan.volley2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.example.sahan.volley2.Adapter.CustomListAdapter;
import com.example.sahan.volley2.app.AppController;
import com.example.sahan.volley2.Model.lot;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
//json url
private static final String url =
"http://localhost/smartpark/get_info.php";
private ProgressDialog pDialog;
private List<lot> lotList = new ArrayList<lot>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, lotList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
// getActionBar().setBackgroundDrawable(
// new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
lot lot = new lot();
lot.setLot(obj.getString("lot"));
lot.setSpaces(obj.getString("spaces"));
lot.setRates(obj.getString("rates"));
// adding movie to movies array
lotList.add(lot);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
****** ****** CustomListAdapter
package com.example.sahan.volley2.Adapter;
/**
* Created by Sahan on 2015-10-27.
*/
import com.example.sahan.volley2.R;
import com.example.sahan.volley2.app.AppController;
import com.example.sahan.volley2.Model.lot;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<lot> lotItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<lot> lotItems) {
this.activity = activity;
this.lotItems = lotItems;
}
@Override
public int getCount() {
return lotItems.size();
}
@Override
public Object getItem(int location) {
return lotItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
TextView lot = (TextView) convertView.findViewById(R.id.lot);
TextView spaces = (TextView) convertView.findViewById(R.id.spaces);
TextView rates = (TextView) convertView.findViewById(R.id.rates);
// getting movie data for the row
lot m = lotItems.get(position);
// title
lot.setText(m.getLot());
// rating
spaces.setText("Spaces: " + String.valueOf(m.getSpaces()));
// release year
rates.setText(String.valueOf(m.getRates()));
return convertView;
}
}
**** **** AppController.java
package com.example.sahan.volley2.app;
/**
* Created by Sahan on 2015-10-27.
*/
import com.example.sahan.volley2.utils.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
**** **** lot.java
package com.example.sahan.volley2.app;
/**
* Created by Sahan on 2015-10-27.
*/
import com.example.sahan.volley2.utils.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
**** **** LruBitmapCache
package com.example.sahan.volley2.utils;
/**
* Created by Sahan on 2015-10-27.
*/
import com.android.volley.toolbox.ImageLoader.ImageCache;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
**** ****的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sahan.volley2" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.example.sahan.volley2.app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
**** **** activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />
**** **** list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp" >
<!-- Lot Title -->
<TextView
android:id="@+id/lot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/lot"
android:textStyle="bold" />
<!-- Rating -->
<TextView
android:id="@+id/spaces"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lot"
android:layout_marginTop="1dip"
android:textSize="@dimen/spaces" />
<!-- Genre -->
<TextView
android:id="@+id/rates"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spaces"
android:layout_marginTop="5dp"
android:textColor="@color/rates"
android:textSize="@dimen/rates" />
**** ****的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.sahan.volley2"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
}
答案 0 :(得分:0)
看起来你的Lot.java由于某种原因与AppController.java相同。 你的Lot类应该是这样的:
public class Lot {
private String mString;
private String mSpaces;
private String mRates;
public Lot() {
}
public String getString() {
return mString;
}
public void setString(String string) {
mString = string;
}
public String getSpaces() {
return mSpaces;
}
public void setSpaces(String spaces) {
mSpaces = spaces;
}
public String getRates() {
return mRates;
}
public void setDirector(String rates) {
mRates = rates;
}
}
此Lot类是您在MainActivity的
中存储的节点/项目private List<Lot> lotList = new ArrayList<Lot>();