我被困在这条线上5天了。当我为它设置适配器时,你想帮助我为什么说Null Pointer Exception。
这是ListFileController
public class ListFileController extends ArrayAdapter<Files> {
private Context context;
private int layoutId;
ArrayList<Files> array;
public ListFileController(Context context, int layoutId, ArrayList<Files> array) {
super(context, layoutId, array);
this.context = context;
this.layoutId = layoutId;
this.array = array;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater;
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null){
holder = new ViewHolder();
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_file_layout, parent, false);
holder.icon = (ImageView) rowView.findViewById(R.id.icon);
holder.fileName = (TextView) rowView.findViewById(R.id.fileName);
holder.size = (TextView) rowView.findViewById(R.id.fileSize);
rowView.setTag(holder);
}
else{
holder = (ViewHolder) rowView.getTag();
}
Files i = array.get(position);
if (array.get(position).isFolder() == 1){
holder.icon.setImageResource(R.drawable.folder);
}
else if (array.get(position).isFolder() == 0){
if (array.get(position).getFileType().contains("image") || array.get(position).getFileType().contains("picture"))
holder.icon.setImageResource(R.drawable.image);
else if (array.get(position).getFileType().contains("sound")|| array.get(position).getFileType().contains("music"))
holder.icon.setImageResource(R.drawable.audio);
else if (array.get(position).getFileType().contains("video")|| array.get(position).getFileType().contains("film"))
holder.icon.setImageResource(R.drawable.video);
else
holder.icon.setImageResource(R.drawable.file);
}
else
holder.icon.setImageResource(R.drawable.back);
holder.fileName.setText(array.get(position).getFileName());
holder.size.setText(array.get(position).getFileName());
return rowView;
}
public static class ViewHolder{
ImageView icon;
TextView fileName;
TextView size;
}
}
这是我的MainActivity
public class MainActivity extends Activity implements View.OnClickListener {
private DropboxAPI<AndroidAuthSession> dropbox;
final static private String ROOT = "/";
private final static String APP_NAME = "dropbox_prefs";
final static private String APP_KEY = "x7bv6zets19iqt4";
final static private String APP_SECRET = "i95y5a09v0wlopu";
private static final int REQUEST_PATH = 1;
private ArrayAdapter<Files> controller = null;
private AlertDialog.Builder alter;
private AlertDialog.Builder inputbuilder;
private String deviceFilePath;
private String deviceFilename;
public ArrayList<Files> listOfFiles = new ArrayList<Files>();
public ArrayList<Files> fileFromDropbox = new ArrayList<Files>();
public ArrayList<String> listOfFolder = new ArrayList<String>();
private Files selected;
private String currentPath = ROOT;
private String previousPath = "";
public boolean isLoggedIn;
public Button logInBut;
public Button uploadBut;
public Button listFilesBut;
ViewStub listView;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isLoggedIn = false;
// Creating button on the screen
logInBut = (Button) findViewById(R.id.logInBut);
logInBut.setOnClickListener(this);
uploadBut = (Button) findViewById(R.id.uploadBut);
uploadBut.setOnClickListener(this);
listView = (ViewStub) findViewById(R.id.list_stub);
listView.inflate();
list = (ListView) findViewById(R.id.list_stub);
// Create an array to store item >> Init 1 item to make array not null
fileFromDropbox.add(new Files("1st", "test", 0));
listOfFiles.add(new Files ("1st", "test", 0));
controller = new ListFileController(MainActivity.this, android.R.layout.simple_list_item_1,
listOfFiles);
list.setAdapter(controller);
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selected = controller.getItem(position);
if (selected.isFolder() == 1) {
folderOptionDialog();
}
return true;
}
});
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selected = controller.getItem(position);
if (selected.isFolder() == 1) {
currentPath = selected.getPath();
updateList(selected.getPath());
} else if (selected.isFolder() == 2) {
updateList(selected.getParentPath());
} else {
currentPath = selected.getParentPath();
fileOptions();
}
controller.notifyDataSetChanged();
}
});
// Authentication dropbox API account
checkLogIn(false);
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
dropbox = new DropboxAPI<AndroidAuthSession>(session);
dropbox.getSession().startOAuth2Authentication(MainActivity.this);
ConnectivityManager connectivityChecker = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//For 3G check
boolean is3G = connectivityChecker.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting();
//For WiFi Check
boolean isWifi = connectivityChecker.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting();
if (!is3G && !isWifi)
Toast.makeText(MainActivity.this, "Please connect to the internet", Toast.LENGTH_LONG).show();
if (!isWifi)
Toast.makeText(MainActivity.this, "3G service is currently not available, please connect via WIFI", Toast.LENGTH_LONG).show();
}
private void setAdapter() {
controller = new ListFileController(this,android.R.layout.simple_list_item_1, listOfFiles);
list.setAdapter(controller);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selected = controller.getItem(position);
if (selected.isFolder() == 1) {
currentPath = selected.getPath();
updateList(selected.getPath());
} else if (selected.isFolder() == 2) {
updateList(selected.getParentPath());
} else {
currentPath = selected.getParentPath();
fileOptions();
}
controller.notifyDataSetChanged();
}
});
list.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selected = controller.getItem(position);
if (selected.isFolder() == 1) {
folderOptionDialog();
}
return true;
}
});
}
这是我的XML文件,其中包含列表。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/logInBut"
android:layout_marginLeft = "15dp"
android:layout_marginRight="15dp"
android:layout_width="373dp"
android:layout_height="243dp"
android:textColor="#43b8e3"
android:text="LOG IN"
android:textSize="100dp"
android:layout_gravity="center_horizontal"
android:enabled="false" />
<Button
android:id="@+id/uploadBut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UPLOAD"
android:textColor="#43b8e3"
android:textSize="80dp"
android:layout_weight="0.01"
android:layout_gravity="center_horizontal" />
<ViewStub
android:id="@+id/list_stub"
android:inflatedId="@+id/showlayout"
android:layout="@layout/activity_list_file"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
</merge>
最后一个是布局
<?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">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:3)
这是您的ListView XML:
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是你的Java,创建一个对它的引用:
list = (ListView) findViewById(R.id.list_stub);
您使用的是错误的资源标识符。将行更改为:
list = (ListView) findViewById(android.R.id.list);