我正在开发一个Android应用程序,它使用Fragment和listView在HomeActivity上生成两个列表,并且还可以自定义使用NavigationViewer作为菜单。
MainActivity:
public class Fragment1 extends ListFragment {
PackageManager pm;
ListView apList;
int c = 1;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public Fragment1() {
}
public static Fragment1 newInstance(String param1, String param2) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
apList=(ListView) getActivity().findViewById(R.id.listView);
pm = getActivity().getPackageManager();
List<PackageInfo> packageList = pm
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
for (PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if (!b) {
packageList1.add(pi);
}
}
apList.setAdapter(new ApkAdapter(this, packageList1, pm, c));
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
if((pkgInfo.applicationInfo.flags & (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) >0)
{
return true;
}
else{
return true;
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
片段1:
public class Fragment2 extends ListFragment {
int c=0;
String str[]= {"Wifi","Bluetooth"};
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle s) {
View view = inflater.inflate(R.layout.fragment_2, vg, false);
List<String> l = new ArrayList<>();
l.add("Wifi");
l.add("Bluetooth");
ApkAdapter ad = new ApkAdapter(this,l ,c);
setListAdapter(ad);
return view;
}
public void onCreate(Bundle s) {
ListView l1;
l1=(ListView)getActivity().findViewById(R.id.listView1);
List<String> l=new ArrayList<>();
l.add("Wifi");
l.add("Bluetooth");
ApkAdapter ad=new ApkAdapter(this,l,c);
l1.setAdapter(ad);
}
}
片段2:
public class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList;
List<String> s1;
Fragment1 context;
Fragment2 context1;
PackageManager packageManager;
Switch s;
int c;
String appName;
String str[];
int img[]={R.drawable.wifi,R.drawable.bluetooth};
public ApkAdapter(Fragment1 context, List<PackageInfo> packageList,
PackageManager packageManager, int c) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
this.c=c;
}
public ApkAdapter(Fragment2 context,List<String> s,int c)
{
super();
this.context1=context;
this.s1=s;
this.c=c;
}
private class ViewHolder {
TextView apkName;
ImageView i;
}
public int getCount() {
if (c == 1) {
return packageList.size();
}
else if(c==0)
{
return str.length;
}
else return 0;
}
public Object getItem(int position) {
if(c==1){
return packageList.get(position);
}
else
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder,h1;
LayoutInflater inflater = context.getActivity().getLayoutInflater();
LayoutInflater inflater1 = context1.getActivity().getLayoutInflater();
ArrayList<String> ar = new ArrayList<String>();
if (convertView == null) {
if(c==0) {
convertView = inflater.inflate(R.layout.apklist_item, null);
}
else if(c==1)
{
convertView = inflater1.inflate(R.layout.apklist_item, null);
}
holder = new ViewHolder();
holder.apkName = (TextView) convertView.findViewById(R.id.appname);
holder.i=(ImageView)convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
if(c==0) {
int j = 0;
while (j < 2) {
int ser=(Integer) getItem(position);
appName=s1.get(ser);
Drawable dr = context.getResources().getDrawable(img[j]);
dr.setBounds(0,0,10,10);
resize(dr);
holder.apkName.setCompoundDrawablesWithIntrinsicBounds(img[j], 0, 0, 0);
holder.apkName.setCompoundDrawablePadding(3);
holder.apkName.setText("\n" + " " + appName);
j++;
}
}
else if(c==1) {
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 100, 100);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(25);
holder.apkName.setText("\n" + " " + appName);
}
holder.i.setScaleY(0.5f);
holder.i.setImageResource(R.drawable.images);
}
catch(Exception e) {
e.printStackTrace();
}
return convertView;
}
public Drawable resize(Drawable i)
{
Bitmap b=((BitmapDrawable)i).getBitmap();
Bitmap br=Bitmap.createScaledBitmap(b,10,10,false);
return new BitmapDrawable(Resources.getSystem(),br);
}
}
ApkAdapter:
getPackageInfo()
还有一个AppData类具有setPackageInfo()
和<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
方法。
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/fragment_2"
android:name="com.example.sardwal.applock.Fragment2"
tools:layout="@layout/fragment_2" />
<fragment
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/fragment_1"
android:name="com.example.sardwal.applock.Fragment1"
tools:layout="@layout/fragment_1"
android:layout_marginTop="30dp" />
</LinearLayout>
content_main.xml:
FATAL EXCEPTION: main
Process: com.example.sardwal.applock, PID: 21919
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sardwal.applock/com.example.sardwal.applock.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.sardwal.applock.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.sardwal.applock.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
fragment_1.xml和fragment_2.xml有listView和TextView,而前者使用的是frameLayout,后者使用的是linear.Both的id标签设置为android:idlist
apklist_item.xml: 它有一个textview和ImageView。 由于字数限制,无法放置他们的代码。我想它不会有任何区别。
我在logcat中遇到以下异常,并且不知道如何解决它们:
E/AppOnlineConfigurationUpdateTask: Get app configuration fail. Target host must not be null, or set in parameters. scheme=null, host=null, path=/reaper/server/appparams
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=/reaper/server/appparams
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:603)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:299)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:601)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
at com.lenovo.lps.reaper.sdk.i.b.a(Unknown)
at com.lenovo.lps.reaper.sdk.i.a.run(Unknown)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
E/ConfigurationUpdateTask: Same stuff IllegalStateException
E/SurfaceFlinger: strok layer name=none
E/EventReportHandler: Same stuff IllegalStateException
E/SurfaceFlinger: strok layer name=none
E/WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 1540 num clients 10
E/WifiTrafficPoller: packet count Tx=1476881 Rx=1685658
E/InputMethodManagerService: Ignoring updateSystemUiLocked due to an invalid token. uid:1000 token:null
E/SwitchMobileDataTile: mobile data not ready or disabled
E/InputDispatcher: channel 'f377d75 (server)' ~ Channel is unrecoverably broken and will be disposed!
E/MountService: setDefaultPath new path=/storage/emulated/0
除此之外,最近当我在我的lenovo设备上运行相同的代码时,我在logcat上遇到以下错误错误:
{{1}}
我遇到了这些错误而没有得到如何解决它们。 感谢
答案 0 :(得分:1)
应用程序崩溃,因为您正在片段中的onCreate方法中查找列表。你应该在片段中使用这样的东西:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { ListView l1;
l1=(ListView)view.findViewById(R.id.listView1);
List<String> l=new ArrayList<>();
l.add("Wifi");
l.add("Bluetooth");
ApkAdapter ad=new ApkAdapter(this,l,c);
l1.setAdapter(ad);}
这是在Fragment的onCreateView方法之后调用的,因此你将设置Fragment的布局,你将不会寻找null ListView。
答案 1 :(得分:0)
在您的情况下,为了工作,您必须具备以下条件: 片段1 xml看起来应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
编辑:
片段2 xml应该几乎相同:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
片段的创建应该是相同的:
onCreate当然来自Activity。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment1 f1 = new Fragment1();
Fragment2 f2 = new Fragment2();
ft.replace(R.id.fragment_1, f1);
ft.replace(R.id.fragment_2, f2);
ft.commit();
}
接下来在onCreateView中你应该有类似的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle s) {
View view = inflater.inflate(R.layout.fragment_2, vg, false);
List<String> l = new ArrayList<>();
l.add("Wifi");
l.add("Bluetooth");
ApkAdapter ad = //choose whichever constructor you want.
setListAdapter(ad);
return view;
}
活动布局xml应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<fragment
android:id="@+id/fragment_1"
android:name="com.example.atstanescu.myapplication.Fragment1"
android:layout_width="match_parent"
android:layout_height="200dp"
tools:layout="@layout/fragment_2" />
<fragment
android:id="@+id/fragment_2"
android:name="com.example.atstanescu.myapplication.Fragment2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
tools:layout="@layout/fragment_1" />
</LinearLayout>
One additional note, your adapter looks really buggy. I needed to reduce it a lot to see some issues. For example you check for c==0 and c==1 but what happens if you have 2, 3.