我的ListView应用程序每次都崩溃并显示" app已停止"

时间:2017-09-17 07:18:25

标签: android json listview adapter

该应用程序具有listView活动,并显示从本地服务器获取的项目。在模拟器上运行应用程序时,它每次都会崩溃并显示"应用程序已停止"。使用Volley,picasso。请验证我的代码。谢谢。 我的json输出是

{"list":[{"name":"messi","price":"hundred","image":"http:\/\/192.168.28.2\/img\/messi.jpeg"},{"name":"goat","price":"twohundred","image":"http:\/\/192.168.28.2\/img\/goat.jpg"},{"name":"cristiano","price":"threehundred","image":"http:\/\/192.168.28.2\/img\/cr7.jpg"}]}

我的主要活动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">
    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

我的自定义布局xml是list_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvprice"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView"/>

</LinearLayout>

获取数据:player.java

package com.example.selvam.recyclerlist;

/**
 * Created by SELVAM on 16-09-2017.
 */

public class Player {
    String name, price,imageUrl;

    public Player(String name, String price, String imageUrl) {
        this.name = name;
        this.price = price;
        this.imageUrl = imageUrl;
    }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }

    public String getImageUrl() {
        return imageUrl;
    }
}

ListView适配器

package com.example.selvam.recyclerlist;

import android.content.Context;


import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by SELVAM on 16-09-2017.
 */

public class ListAdapter extends ArrayAdapter<Player> {
    private List<Player> playerList;
    private Context context;
    public ListAdapter( List<Player> playerList,Context context){
    super(context,R.layout.list_items,playerList);
    this.context=context;
    this.playerList=playerList;

}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View listViewItem = inflater.inflate(R.layout.list_items, null, true);

        //getting text views
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.tvName);
        TextView textViewPrice = (TextView) listViewItem.findViewById(R.id.tvprice);
        ImageView imageView = (ImageView) listViewItem.findViewById(R.id.imageView);

        //Getting the hero for the specified position
        Player player = playerList.get(position);

        //setting hero values to textviews
        textViewName.setText(player.getName());
        textViewPrice.setText(player.getPrice());
        Picasso.with(context)
                .load(player.getImageUrl())
                .resize(50, 50)
                .centerCrop()
                .into(imageView);

        //returning the listitem
        return listViewItem;

    }

}

主要活动

package com.example.selvam.recyclerlist;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String JSON_URL="http://192.168.28.2/signup/activities/getDataToListview.php";
    ListView listView;
    List<Player>players;
    ProgressDialog progressDialog;



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

        listView=(ListView)findViewById(R.id.myListView);
        players=new ArrayList<>();
        progressDialog=new ProgressDialog(this);
        loadPlayerList();

    }
    public void loadPlayerList(){
        progressDialog.setMessage("loading...");
        progressDialog.show();
        StringRequest stringRequest=new StringRequest(Request.Method.GET, JSON_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray jsonArray=jsonObject.getJSONArray("list");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1=jsonArray.getJSONObject(i);

                        Player player=new Player(jsonObject1.getString("name"),jsonObject1.getString("price"),jsonObject1.getString("image"));

                        players.add(player);
                    }
                    ListAdapter listAdapter=new ListAdapter(players,getApplicationContext());
                    listView.setAdapter(listAdapter);




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

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

我的清单文件是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.selvam.recyclerlist">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的logcat是:

09-17 08:55:19.600 4326-4326/? I/art: Late-enabling -Xcheck:jni
09-17 08:55:19.601 4326-4326/? W/art: Unexpected CPU variant for X86 using defaults: x86
09-17 08:55:19.640 4326-4326/? W/System: ClassLoader referenced unknown path: /data/app/com.example.selvam.recyclerlist-1/lib/x86
09-17 08:55:19.647 4326-4326/? I/InstantRun: starting instant run server: is main process
09-17 08:55:19.651 4326-4326/? I/art: Rejecting re-init on previously-failed class java.lang.Class<com.example.selvam.recyclerlist.MainActivity>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
09-17 08:55:19.651 4326-4326/? I/art:     at java.lang.Class java.lang.VMClassLoader.findLoadedClass!(java.lang.ClassLoader, java.lang.String) (VMClassLoader.java:-2)
09-17 08:55:19.651 4326-4326/? I/art:     at java.lang.Class java.lang.ClassLoader.findLoadedClass(java.lang.String) (ClassLoader.java:742)
09-17 08:55:19.651 4326-4326/? I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:362)
09-17 08:55:19.651 4326-4326/? I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at android.app.Activity android.app.Instrumentation.newActivity(java.lang.ClassLoader, java.lang.String, android.content.Intent) (Instrumentation.java:1078)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2538)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.os.Looper.loop() (Looper.java:154)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:865)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:755)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.example.selvam.recyclerlist-1/base.apk",
zip file "/data/app/com.example.selvam.recyclerlist-1/split_lib_slice_6_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.selvam.recyclerlist-1/lib/x86, /system/lib, /vendor/lib]]
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.VMClassLoader.findLoadedClass!(java.lang.ClassLoader, java.lang.String) (VMClassLoader.java:-2)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.ClassLoader.findLoadedClass(java.lang.String) (ClassLoader.java:742)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:362)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at android.app.Activity android.app.Instrumentation.newActivity(java.lang.ClassLoader, java.lang.String, android.content.Intent) (Instrumentation.java:1078)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2538)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
09-17 08:55:19.651 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.os.Looper.loop() (Looper.java:154)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:865)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist I/art:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:755)
09-17 08:55:19.652 4326-4326/com.example.selvam.recyclerlist D/AndroidRuntime: Shutting down VM
09-17 08:55:19.653 4326-4326/com.example.selvam.recyclerlist E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.selvam.recyclerlist, PID: 4326
   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.selvam.recyclerlist/com.example.selvam.recyclerlist.MainActivity}:
   java.lang.ClassNotFoundException: Didn't find class "com.example.selvam.recyclerlist.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.selvam.recyclerlist-1/base.apk",
   zip file "/data/app/com.example.selvam.recyclerlist-1/split_lib_slice_6_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.selvam.recyclerlist-1/lib/x86, /system/lib, /vendor/lib]]
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6077)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.selvam.recyclerlist.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.selvam.recyclerlist-1/base.apk",
    zip file "/data/app/com.example.selvam.recyclerlist-1/split_lib_slice_6_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.selvam.recyclerlist-1/lib/x86, /system/lib, /vendor/lib]]
       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
       at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:154) 
       at android.app.ActivityThread.main(ActivityThread.java:6077) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
       at java.lang.VMClassLoader.findLoadedClass(Native Method)
       at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:742)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:362)
            ... 12 more
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.example.selvam.recyclerlist-1/base.apk",
    zip file "/data/app/com.example.selvam.recyclerlist-1/split_lib_slice_6_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.selvam.recyclerlist-1/lib/x86, /system/lib, /vendor/lib]]
       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

       ... 15 more

1 个答案:

答案 0 :(得分:1)

使用getView(...)的{​​{1}}方法。 试试这个(从ListAdapter更改为true):

false

请访问here以阅读有关View listViewItem = inflater.inflate(R.layout.list_items, null, false); 方法的更多信息。