我正在使用 ListActivity 但它不使用自定义适配器并在我使用它时崩溃。
此处代码:
FilesMenu.java
package com.handyapp.filesaggregator;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FilesMenu extends ListActivity {
public static Context con;
Map<String,File> dirMap;
ArrayList<String> dirs;
File[] files;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//this.
SharedPreferences prefs=(SharedPreferences) getSharedPreferences("OFFSET",MODE_PRIVATE);
con=this;
File sdCard=Environment.getExternalStorageDirectory();
fileOp(sdCard);
setContentView(R.layout.activity_files_menu);
setListAdapter(new MyAdapter<String>(con,R.layout.item_list,R.id.txt1,dirs));
getListView().setSelection(prefs.getInt("OFFSET_LIST",0));
this.getListView().setFastScrollEnabled(true);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
new FilesCollectorTask().execute(dirMap.get(dirs.get(position)));
return false;
}
});
new AlertDialog.Builder(con)
.setIcon(R.drawable.exclmark)
.setMessage("INSTRUCTIONS:\n1. This is just a BETA release"
+"\n2. To organize a particular folder, Long click the folder and wait"
+"\n3. And i repeat again, this is just a beta release, big folders take quite a lot of time to finish,PLEASE be patient"
+"\n4. creator:Abdul Hannan Kanji :P")
.setNeutralButton("Got it!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
private void fileOp(File root)
{
files=root.listFiles();
dirs=new ArrayList<String>();
dirMap=new HashMap<String,File>();
dirs.add("...");
dirMap.put("...", root.getParentFile());
if(files!=null)
{
for(File file : files)
{
if(file!=null&&file.isDirectory())
{
dirs.add(file.getName());
dirMap.put(file.getName(), file);
}
}
}
}
public void onListItemClick(ListView parent,View v,int position,long id)
{
try {
fileOp(dirMap.get(dirs.get(position)));
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//int pos=this.getListView().getBaseline();
SharedPreferences prefs=(SharedPreferences) con.getSharedPreferences("OFFSET", MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putInt("OFFSET_LIST", position);
editor.commit();
parent.setAdapter(new MyAdapter<String>(con,R.layout.item_list,R.id.txt1,dirs));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_files_menu, menu);
return true;
}
public class FilesCollectorTask extends AsyncTask<File, Void, Boolean> {
private ProgressDialog dialog;
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
dialog.dismiss();
new AlertDialog.Builder(con)
.setMessage("Operation Unsuccessful!Make sure you have enough memeory in your external storage")
.setNeutralButton("Dismiss", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.setIcon(R.drawable.exclmark);
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog=ProgressDialog.show(con, "Aggregating Files", "Please wait...");
super.onPreExecute();
}
@Override
protected Boolean doInBackground(File... params) {
// TODO Auto-generated method stub
if(params==null || params.length==0)
{
this.cancel(true);
}
try {
//Toast.makeText(con,params[0].getName(), Toast.LENGTH_SHORT).show();
FileOperation.doWithFiles(params[0]);
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(con, "IOException ", Toast.LENGTH_SHORT).show();
this.cancel(true);
return false;
}
return true;
}
}
}
我的适配器.java
public class MyAdapter<T> extends ArrayAdapter<T> {
Context con;
ArrayList<String> items;
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return (T)items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row;
TextView tv;
LayoutInflater inflator=(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
row=inflator.inflate(R.layout.item_list,parent);
}
else
{
row=convertView;
}
tv=(TextView) row.findViewById(R.id.txt1);
tv.setText(items.get(position));
return row;
}
public MyAdapter(Context context,int resource,int textViewResourceId,
ArrayList<String> objects) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
con=context;
items=objects;
}
}
activity_files_menu.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"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:background="@drawable/ambience"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
item_list.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt1"
android:alpha="0"
android:textColor="#D2DCEE"
android:drawableLeft="@drawable/folder_small"/>
</LinearLayout>
日志猫
01-15 14:03:50.586: D/dalvikvm(579): GC_EXTERNAL_ALLOC freed 69K, 52% free 2632K/5379K, external 2002K/2137K, paused 78ms
01-15 14:03:51.096: D/AndroidRuntime(579): Shutting down VM
01-15 14:03:51.096: W/dalvikvm(579): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-15 14:03:51.126: E/AndroidRuntime(579): FATAL EXCEPTION: main
01-15 14:03:51.126: E/AndroidRuntime(579): java.lang.RuntimeException: Binary XML file line #6: You must supply a layout_width attribute.
01-15 14:03:51.126: E/AndroidRuntime(579): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3598)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3678)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1400)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1326)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:47)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.LayoutInflater.rInflate(LayoutInflater.java:625)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-15 14:03:51.126: E/AndroidRuntime(579): at com.handyapp.filesaggregator.MyAdapter.getView(MyAdapter.java:42)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.ListView.makeAndAddView(ListView.java:1745)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.ListView.fillSpecific(ListView.java:1290)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.ListView.layoutChildren(ListView.java:1588)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.View.layout(View.java:7175)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.View.layout(View.java:7175)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.View.layout(View.java:7175)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.View.layout(View.java:7175)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.os.Looper.loop(Looper.java:123)
01-15 14:03:51.126: E/AndroidRuntime(579): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-15 14:03:51.126: E/AndroidRuntime(579): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 14:03:51.126: E/AndroidRuntime(579): at java.lang.reflect.Method.invoke(Method.java:507)
01-15 14:03:51.126: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 14:03:51.126: E/AndroidRuntime(579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 14:03:51.126: E/AndroidRuntime(579): at dalvik.system.NativeStart.main(Native Method)
如果不可能,还有其他更简单的方法吗?编辑:
包含 layout_width 属性后,出现以下错误:
logCat
01-15 14:22:38.246: D/dalvikvm(614): GC_EXTERNAL_ALLOC freed 70K, 52% free 2632K/5379K, external 2002K/2137K, paused 89ms
01-15 14:22:38.826: D/AndroidRuntime(614): Shutting down VM
01-15 14:22:38.826: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-15 14:22:38.846: E/AndroidRuntime(614): FATAL EXCEPTION: main
01-15 14:22:38.846: E/AndroidRuntime(614): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.AdapterView.addView(AdapterView.java:461)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.LayoutInflater.inflate(LayoutInflater.java:416)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-15 14:22:38.846: E/AndroidRuntime(614): at com.handyapp.filesaggregator.MyAdapter.getView(MyAdapter.java:42)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.ListView.makeAndAddView(ListView.java:1745)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.ListView.fillSpecific(ListView.java:1290)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.ListView.layoutChildren(ListView.java:1588)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.View.layout(View.java:7175)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.View.layout(View.java:7175)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.View.layout(View.java:7175)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.View.layout(View.java:7175)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:123)
01-15 14:22:38.846: E/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-15 14:22:38.846: E/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 14:22:38.846: E/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:507)
01-15 14:22:38.846: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 14:22:38.846: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 14:22:38.846: E/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method)