我正在创建一个ArrayAdapter
,我得到一个NullPointerException
,我不知道为什么。在过去的几天里,我一直在发布这个文件很多次,因为我是Android API的新手,我正在尝试习惯它的工作原理。以下是打印堆栈
03-26 02:00:35.263: E/AndroidRuntime(1783): FATAL EXCEPTION: main
03-26 02:00:35.263: E/AndroidRuntime(1783): java.lang.NullPointerException
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ListView.makeAndAddView(ListView.java:1840)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ListView.fillDown(ListView.java:675)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ListView.fillFromTop(ListView.java:736)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.ListView.layoutChildren(ListView.java:1655)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.View.layout(View.java:14289)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.View.layout(View.java:14289)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-26 02:00:35.263: E/AndroidRuntime(1783): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.View.layout(View.java:14289)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.View.layout(View.java:14289)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.os.Handler.handleCallback(Handler.java:730)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.os.Looper.loop(Looper.java:137)
03-26 02:00:35.263: E/AndroidRuntime(1783): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-26 02:00:35.263: E/AndroidRuntime(1783): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 02:00:35.263: E/AndroidRuntime(1783): at java.lang.reflect.Method.invoke(Method.java:525)
03-26 02:00:35.263: E/AndroidRuntime(1783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-26 02:00:35.263: E/AndroidRuntime(1783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-26 02:00:35.263: E/AndroidRuntime(1783): at dalvik.system.NativeStart.main(Native Method)
这是类ListActivity
的代码:
package dev.shaw.MyShoppingPlanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class List_Activity extends ListActivity {
String ListNames[];
boolean isNew = false;
String inputtext ="";
int stop = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListNames = getListNames();
String New[] = {"Create List"};
if(ListNames[0].isEmpty()){
ListNames[0] = "Create List";
isNew = true;
setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem , ListNames));
}else{
setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem , ListNames));
}
}
@Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, pos, id);
if(isNew == true){
isNew = false;
createNewList();
}else{
ShoppingList list = new ShoppingList(ListNames[pos], this);
try {
setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list.toArray(list.getFile())));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void createNewList() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter a Title:");
// Set up the input
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputtext = input.getText().toString();
ShoppingList list = new ShoppingList(inputtext, List_Activity.this);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private String[] getListNames() {
//generates the file containing the list names
File file = new File(this.getFilesDir() +File.separator +"Lists");
String names[] = new String[10];
System.out.println(file.getAbsolutePath());
if(file.isDirectory()){
System.out.println("File is a directory");
File lists[] = file.listFiles();
if(lists!=null){
for(int i = 0; i < lists.length; i ++){
names[i] = lists[i].getName();
}
}else{
names[0] = "Create New List";
}
}else{
System.out.println("File is not a directory");
}
/*file.setReadable(true);
file.setWritable(true);
if(file.exists()){
System.out.println("List file exists");
}else{
System.out.println("List file doesn't exists");
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStreamReader is = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(is);
String line;
String names[] = new String[100];
int i = 1;
try {
while(true){
line = br.readLine();
if(line == null){
break;
}
else{
try{
names[i] = line;
}
catch(NullPointerException e){
e.printStackTrace();
}
i++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return names;
}
}
此活动xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="dev.shaw.MyShoppingPlanner.List_Activity"
android:background="@drawable/lined_paper_blank"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button_createlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_list" />
<Button
android:id="@+id/button_deletelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete_list" />
</LinearLayout>
答案 0 :(得分:1)
首先,您需要在Activity
内设置布局,通常采用onCreate
方法。我没有看到这个,添加:
// just after this line
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_layout);
然后,我刚刚夸大了你的布局,你需要在这个布局中创建你的ListView
,如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="dev.shaw.MyShoppingPlanner.List_Activity"
android:background="@drawable/lined_paper_blank"
tools:ignore="MergeRootFrame" >
...
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
以上是List_Activity
名为list_activity_layout
的布局。您可以在Create Listview With ListActivity上看到一个小例子
希望这有帮助。