我是android开发的新手,正在尝试解决我的应用程序中某些已安装的应用程序的问题。问题是出现一些投射错误
(无法将android.support.constraint.ConstraintLayout强制转换为android.widget.TextView)
我在这里提到过很多帖子,我试图弄清楚,但是没有运气。作为最后的尝试,我将代码发布在这里,希望得到答复。 整个代码发布在下面。
任何帮助将不胜感激
ApplicationAdapter.java
package e.nani.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo > {
private int resourceLayout;
private Context mContext;
List<ApplicationInfo> items;
PackageManager pm ;
ApplicationInfo p;
public ApplicationAdapter(Context context, int resource, List<ApplicationInfo> items) {
super(context, resource, items);
this.resourceLayout = resource;
this.mContext = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.row_view, null);
}
pm=mContext.getPackageManager();
p = items.get(position);
if (p!= null) {
ImageView tt1 = (ImageView)view.findViewById(R.id.img);
TextView tt2= (TextView)view.findViewById(R.id.t1);
if (tt1 != null) {
tt1.setImageDrawable(p.loadIcon(pm));
}
if (tt2 != null) {
tt2.setText(p.packageName);
}
}
return view;
}
}
MainActivity.java
package e.nani.myapplication;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listview;
ApplicationAdapter<ApplicationInfo> adapter;
List<ApplicationInfo> ls=new ArrayList<>();
PackageManager pm;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.lists);
try {
pm= getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
ls.add(packageInfo);
adapter = new ApplicationAdapter<ApplicationInfo>(this, R.layout.row_view, ls);
listview.setAdapter(adapter);
Log.i("added", String.valueOf(pm.getClass().getName()));
Log.i("middle",packageInfo.packageName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/lists"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</android.widget.RelativeLayout>
row_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
``崩溃报告
Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:432)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)
at e.nani.myapplication.ApplicationAdapter.getView(ApplicationAdapter.java:49)
at android.widget.AbsListView.obtainView(AbsListView.java:2408)
at android.widget.ListView.makeAndAddView(ListView.java:2126)
at android.widget.ListView.fillDown(ListView.java:851)
at android.widget.ListView.fillDown(ListView.java:833)
at android.widget.ListView.fillFromTop(ListView.java:921)
at android.widget.ListView.layoutChildren(ListView.java:1900)
at android.widget.AbsListView.onLayout(AbsListView.java:2207)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:754)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2991)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2504)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1635)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7795)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1172)
at android.view.Choreographer.doCallbacks(Choreographer.java:984)
at android.view.Choreographer.doFrame(Choreographer.java:809)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1158)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
尝试使用ViewHolder模式:
static class ViewHolder {
ImageView tt1;
TextView tt2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.row_view, null);
holder = new ViewHolder();
holder.tt1 = (ImageView)view.findViewById(R.id.img);
holder.tt2 = (TextView)view.findViewById(R.id.t1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
pm = mContext.getPackageManager();
p = items.get(position);
if (p != null) {
holder.tt1.setImageDrawable(p.loadIcon(pm));
holder.tt2.setText(p.packageName);
}
return view;
}