Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams

时间:2015-07-24 02:15:27

标签: java android android-layout android-gridview

我在较旧的Android手机(Android 4.4.2)上运行我的应用时出现此错误。该代码在新手机(Android 5.0)上运行,并且不会抛出错误。这是完整的错误。

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot     be cast to android.widget.AbsListView$LayoutParams
        at android.widget.GridView.onMeasure(GridView.java:1051)
        at android.view.View.measure(View.java:17388)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17388)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17388)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17388)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
        at android.view.View.measure(View.java:17388)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2217)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1553)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1238)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6473)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:573)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

当我尝试在我的XML布局中找到GridView并将其放入MainActivity.java中的ViewGroup时,就会发生这种情况。

继承我的主要活动:

package com.example.eric.myapplication;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.ParseFacebookUtils;        
import com.parse.SaveCallback;

import android.widget.AbsListView.LayoutParams;
import org.w3c.dom.Text;

import javax.crypto.spec.IvParameterSpec;

public class MainActivity extends Activity implements OnClickListener {
Button settings;
GridView mGridView;
ArrayList<String> abc;
public static final String MyPreferences = "MyPrefs";
ContactAdapter adapter;
public int width;
public int height;
private boolean startPortrait;
private float x1, x2;
private ArrayList<String> extras;


private LinearLayout mView;
private ViewGroup viewGroup;
private RelativeLayout mAppBanner;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mView = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null, false);
    mAppBanner = (RelativeLayout) mView.findViewById(R.id.mAppBanner);
    ((ImageView) mAppBanner.findViewById(R.id.titleImage)).setImageResource(R.drawable.mbanner);

    viewGroup = mView;
    mGridView = (GridView) mView.findViewById(R.id.gridView1);

    abc = new ArrayList<String>();

    viewGroup.removeView(mAppBanner);
    viewGroup.removeView(mGridView);
    viewGroup.addView(mAppBanner);
    viewGroup.addView(mGridView);
    setContentView(viewGroup);

    AssetCopier ac = new AssetCopier(getAssets());
    ac.start();

    sharedPreferences = getSharedPreferences(MyPreferences, Context.MODE_PRIVATE);

    //Start Packages Download
    extras = new ArrayList<String>();
    if(!sharedPreferences.contains("downloadedPackages")) {
        ArrayList<String> downloadedPackages = new ArrayList<String>();
        SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(downloadedPackages);
        prefsEditor.putString("downloadedPackages", json);
        prefsEditor.commit();
    }
    if(!sharedPreferences.contains("downloadedEmojis")) {
        ArrayList<String> downloadedEmojis = new ArrayList<String>();
        SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(downloadedEmojis);
        prefsEditor.putString("downloadedEmojis", json);
        prefsEditor.commit();
    }
    final String packagesDirPath = Environment.getExternalStorageDirectory().toString() + File.separator + ".mogeePacks";
    File packagesDirFile = new File(packagesDirPath);
    if(!packagesDirFile.exists()) {
        packagesDirFile.mkdirs();
    }
    final Gson gson = new Gson();
    String json = sharedPreferences.getString("downloadedPackages", "");
    final ArrayList<String> downloadedPackages = gson.fromJson(json, ArrayList.class);
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Mogees");
    query.whereNotContainedIn("name", downloadedPackages);
    query.whereEqualTo("OS","android");

    String ejson = sharedPreferences.getString("downloadedEmojis", "");
    final ArrayList<String> downloadedEmojis = gson.fromJson(ejson, ArrayList.class);

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(final List<ParseObject> packageList, ParseException e) {
            if(packageList!=null && !packageList.isEmpty()) {
                for (ParseObject p : packageList) {
                    final ParseObject po = p;
                    final String pDirPath = Environment.getExternalStorageDirectory().toString() + File.separator + ".mogeePacks" + File.separator + p.getObjectId();
                    File pDirFile = new File(pDirPath);
                    pDirFile.mkdirs();
                    ParseFile f = (ParseFile) p.get("mogeeBundle");
                    f.getDataInBackground(new GetDataCallback() {
                        public void done(byte[] data, ParseException e) {
                            if (e == null) {
                                try {
                                    InputStream input = new ByteArrayInputStream(data);
                                    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(input));
                                    byte[] buffer = new byte[1024];
                                    ZipEntry ze;
                                    int count;
                                    String filename;
                                    while ((ze = zis.getNextEntry()) != null) {
                                        filename = ze.getName();

                                        FileOutputStream fout = new FileOutputStream(pDirPath + File.separator + filename);

                                        while ((count = zis.read(buffer)) != -1) {
                                            fout.write(buffer, 0, count);
                                        }

                                        fout.close();
                                        zis.closeEntry();
                                        downloadedEmojis.add(pDirPath + File.separator + filename);
                                    }
                                    zis.close();
                                } catch (Exception ex) {
                                    throw new RuntimeException(ex);
                                }
                            } else {
                                Log.i("ParseException",e.getMessage());
                            }
                            if(downloadedPackages.size()==1) {
                                downloadedPackages.add((String) po.get("name"));
                                endOnCreate(downloadedEmojis,downloadedPackages);
                            } else {
                                downloadedPackages.add((String) po.get("name"));
                            }
                        }
                    });
                }
            } else {
                extras = downloadedEmojis;
                endOnCreate(null,null);
            }

        }
    });



}
public void endOnCreate(ArrayList<String> downloadedEmojis,ArrayList<String> downloadedPackages) {
    if(downloadedEmojis!=null && downloadedPackages!=null) {
        final Gson gson = new Gson();
        SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
        String json1 = gson.toJson(downloadedEmojis);
        prefsEditor.putString("downloadedEmojis", json1);
        String json2 = gson.toJson(downloadedPackages);
        prefsEditor.putString("downloadedPackages", json2);
        prefsEditor.commit();
        extras = downloadedEmojis;
    }
    setValue();
    final Activity mainActivity = this;
    /*runOnUiThread(new Runnable() {
        @Override
        public void run() {*/

    adapter = new ContactAdapter(getApplicationContext(), 0, abc, extras, mainActivity);
    mGridView.setAdapter(adapter);
    /*    }
    });*/
    mGridView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    x2 = event.getX();
                    break;
            }
            return false;
        }
    });
    mGridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(Math.abs(x1-x2)<adapter.getScale()*128) {
                String path;
                if(position<ContactAdapter.mThumbIds.length) {
                    path = Environment.getExternalStorageDirectory().toString()
                            + File.separator + ".GIF" + "/" + ContactAdapter.mThumbIds[position].getName() + ".gif";
                    Map<String, String> dimensions = new HashMap<String, String>();
                    dimensions.put("name", ContactAdapter.mThumbIds[position].getEasyname());
                    dimensions.put("platform", "app");
                    ParseAnalytics.trackEventInBackground("mogee", dimensions);
                } else {
                    path = extras.get(position-ContactAdapter.mThumbIds.length);
                }
                sendGif(path);
            }
        }

    });
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            width = size.x;
            height = size.y;
            int col_wd;
            if (getResources().getConfiguration().orientation == getResources().getConfiguration().ORIENTATION_PORTRAIT) {
                col_wd = (int) (width / 4.0f);
                adapter.setOrientation(true, width);
                findViewById(R.id.gridView1).invalidate();
                startPortrait = true;
            } else {
                col_wd = (int) (width / 7.0f);
                adapter.setOrientation(false, width);
                findViewById(R.id.gridView1).invalidate();
                startPortrait = false;
            }
            mGridView.setColumnWidth(col_wd);
        }
    });

    settings = (Button) findViewById(R.id.settings);
    settings.setOnClickListener(this);


    Map<String, String> dimensions = new HashMap<String, String>();
    dimensions.put("platform", "app");
    ParseAnalytics.trackAppOpenedInBackground(getIntent());
    ParseAnalytics.trackEventInBackground("launched", dimensions);
    FacebookSdk.sdkInitialize(getApplicationContext());
    ParseFacebookUtils.initialize(getApplicationContext());

    ParsePush.subscribeInBackground("", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
            } else {
                Log.e("com.parse.push", "failed to subscribe for push", e);
            }
        }
    });

}
public ArrayList<String> getExtras() {
    return extras;
}
@Override
public void onClick(View view) {
    ParseFacebookUtils.initialize(getApplicationContext());
    switch(view.getId()) {
        case R.id.settings:
            // take user to the settings pop up menu
            final PopupMenu popup = new PopupMenu(MainActivity.this, settings);
            popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.tutorial:
                            break;
                        case R.id.faq:
                            break;
                        case R.id.feedback:
                            break;
                    }
                    return true;

                }
            });
            popup.show();
            break;
    }
}
public void onLowmemory() {
    Runtime.getRuntime().gc();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tutorial:
            break;
        case R.id.faq:
            break;
        case R.id.feedback:
            break;
    }
    return true;
}

public void sendGif(String path) {
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    File imageFileToShare = new File(path);
    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image!"));
}

private void sendLink() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    String url = new String("Check out my new app...");
    intent.putExtra(Intent.EXTRA_TEXT, url);
    intent.setType("text/plain");
    startActivity(Intent.createChooser(intent, "Share This App!"));
}


public void setValue(){
    for (int i = 0; i < ContactAdapter.mThumbIds.length+extras.size(); i++) {
        abc.add(i+"");
    }
}

class RotateListener extends OrientationEventListener {
    public RotateListener() {
        super(getApplicationContext());
    }
    @Override
    public void onOrientationChanged(int i) {
        int w;
        int h;
        if(startPortrait) {
            w = width;
            h = height;
        } else {
            h = width;
            w = height;
        }
        if (i == 270 || i == 90) {
            adapter.setOrientation(false, h);
            int col_wd = (int) (h / 7.0f);
            mGridView.setColumnWidth(col_wd);
        } else {
            adapter.setOrientation(true, w);
            int col_wd = (int) (w / 4.0f);
            mGridView.setColumnWidth(col_wd);
        }
        mGridView.invalidate();
    }
}
}

以下是主要活动XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mAppBanner"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="vertical">

    <ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/titleImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:layout_alignParentBottom="true" />

    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="left"
        android:background="#00ffffff"
        android:id="@+id/settings"
        android:text="Settings"
        android:textColor="#FFFFFF"
        android:onClick="onButtonClick"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:paddingRight="0dp"
        android:paddingLeft="8dp"
        android:paddingTop="10dp"
        android:paddingBottom="0dp" />
</RelativeLayout>

<GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="none"
        android:scaleType="center"
        android:gravity="center"
        android:clipChildren="true"
        android:drawSelectorOnTop="false"
        android:layout_margin="0dp"
        android:background="#FFFFFF"
        android:listSelector="#FFFFFF"
        android:layout_alignParentRight="true"
        android:scrollbars="none"
        android:layout_alignParentEnd="true">
    </GridView>



</LinearLayout>

这是我的联系适配器类:

package com.example.eric.myapplication;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.view.WindowManager;
import android.view.Display;
import android.graphics.Point;
import android.widget.Toast;

import com.phronesistechne.mogeeapp.widget.GifMovieView;

/**
 * An array adapter that knows how to render views when given CustomData classes
 */
public class ContactAdapter extends ArrayAdapter<String> {
    private LayoutInflater mInflater;
    Context mContext;
    ArrayList<String> mlist;
    //ArrayList<View> viewCache;
    private LruCache moviecache;
    private Activity mainActivity;
    private boolean portrait;
    private int width;
    private final ExecutorService executorService =     Executors.newCachedThreadPool();
    private float scale;
    private ArrayList<String> extras;
    private ArrayList<GifMovieView> gifs;

    public float getScale() {
        return scale;
    }

    @TargetApi(13)
    public ContactAdapter(Context paramContext, int paramInt,
            ArrayList<String> jobList, ArrayList<String> ex, Activity mA) {
        super(paramContext, paramInt);
        this.mContext = paramContext;
        this.mlist = ((ArrayList) jobList);

        mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int cachesize = 1024*1024*64;
        moviecache = new LruCache(cachesize);
        gifs = new ArrayList<GifMovieView>();
        portrait = true;
        extras = ex;
        mainActivity = mA;
    }

    public int getCount() {
        return this.mlist.size();
    }

    @Override
    @TargetApi(13)
    public View getView(final int position, View convertView, ViewGroup parent) {
        //final Holder holder;
        //if (convertView == null) {
        convertView = mInflater.inflate(R.layout.image, parent, false);
        GifMovieView image = (GifMovieView) convertView.findViewById(R.id.gif1);
        image.setActivity(mainActivity);
        if (portrait) {
            image.setScale(width);
        } else {
            image.setLandscapeScale(width);
        }
        scale = image.getScale();
        if(position < mThumbIds.length) {
            image.setMovieResource(mThumbIds[position].getId());
        } else {
            image.setMovieResource(extras.get(position-mThumbIds.length));
        }
        gifs.add(image);
        return image;
        /*try {
            gifs.set(position, holder.image);
        } catch (IndexOutOfBoundsException e) {
            gifs.add(position, holder.image);
        }*/
    }

    /** View holder for the views we need access to */
    public static class Holder {
        public GifMovieView image;
    }
    public void changePausedState() {
        for(GifMovieView g : gifs) {
            g.setPaused(!g.isPaused());
        }
    }
    public void setOrientation(boolean p, int w) {
        portrait = p;
        width = w;
    }

    public static Emojis [] mThumbIds= {
            new Emojis(R.drawable.wavingsmile_1029a,     R.drawable.preview_wavingsmile_1029a, "wavingsmile_1029a", "wavingsmile"),
            new Emojis(R.drawable.wink_1032a, R.drawable.preview_wink_1035a, "wink_1032a", "wink")
            };


}

1 个答案:

答案 0 :(得分:1)

I fixed my problem, in my contact adapter I was returning the wrong view inside of the getView() function. Changing 'return image;' to 'return convertView;' fixed the problem for me, and it works on all the devices I have been able to test.