我尝试仅在列表视图中的项目名称匹配时才实现启动活动的功能。
这是代码,但点击后,应用程序崩溃。怎么了?
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String selectedValue = adapter.getSelectedItem().toString();
if(selectedValue.equalsIgnoreCase("A"))
{
Intent appInfo = new Intent(MainActivity.this, MapActivity.class);
startActivity(appInfo);
}
}
});
这是活动
public class MainActivity extends Activity {
ListView lv;
ArrayAdapter<MyFile> adapter;
public void list() {
lv = (ListView) findViewById(R.id.listView1);
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/");
File[] filelist = dir.listFiles();
ArrayList<MyFile> theNamesOfFiles = new ArrayList<MyFile>();
for (File temp : filelist) {
theNamesOfFiles.add(new MyFile(temp));
}
Collections.sort(theNamesOfFiles, new Comparator<MyFile>() {
@Override
public int compare(MyFile lhs, MyFile rhs) {
return lhs.toString().compareToIgnoreCase(rhs.toString());
}
});
adapter = new ArrayAdapter<MyFile>(this, R.layout.list_row,theNamesOfFiles);
lv.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String selectedValue = adapter.getSelectedItem().toString();
if(selectedValue.equalsIgnoreCase("A"))
{
Intent appInfo = new Intent(MainActivity.this, MapActivity.class);
startActivity(appInfo);
}
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
// setting onItemLongClickListener and passing the position to the
// function
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int position, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Delete Map ?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doDeleteFile(adapter.getItem(position).file);
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
}
public void doDeleteFile(File fileToDelete) {
if (!fileToDelete.isDirectory()) {
try {
if (fileToDelete.delete()) {
System.out.println("File delete operation success");
list();
}
else {
System.out.println("File delete operation failed");
}
} catch (Exception ex) {
System.out.println("Exception :" + ex.getMessage());
}
} else {
System.out.println("It is not a file");
}
}
public class MyFile {
File file;
String fileName;
public MyFile(File file)
{
this.file = file;
this.fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
}
@Override
public String toString() {
return this.fileName;
}
}
}
这是堆栈跟踪
03-13 19:59:36.998: E/AndroidRuntime(14188): FATAL EXCEPTION: main
03-13 19:59:36.998: E/AndroidRuntime(14188): java.lang.NullPointerException
03-13 19:59:36.998: E/AndroidRuntime(14188): at com.example.deletelistitem.MainActivity$2.onItemClick(MainActivity.java:60)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.widget.AbsListView.performItemClick(AbsListView.java:1490)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3275)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.widget.AbsListView$1.run(AbsListView.java:4518)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.os.Handler.handleCallback(Handler.java:725)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.os.Looper.loop(Looper.java:137)
03-13 19:59:36.998: E/AndroidRuntime(14188): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-13 19:59:36.998: E/AndroidRuntime(14188): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 19:59:36.998: E/AndroidRuntime(14188): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 19:59:36.998: E/AndroidRuntime(14188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-13 19:59:36.998: E/AndroidRuntime(14188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-13 19:59:36.998: E/AndroidRuntime(14188): at dalvik.system.NativeStart.main(Native Method)
如果我使用String selectedItem = lv.getSelectedItem()。toString();
,这是堆栈跟踪03-13 21:39:57.381: E/AndroidRuntime(22701): FATAL EXCEPTION: main
03-13 21:39:57.381: E/AndroidRuntime(22701): java.lang.NullPointerException
03-13 21:39:57.381: E/AndroidRuntime(22701): at com.example.deletelistitem.MainActivity$2.onItemClick(MainActivity.java:60)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.widget.AbsListView.performItemClick(AbsListView.java:1490)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3275)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.widget.AbsListView$1.run(AbsListView.java:4518)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.os.Handler.handleCallback(Handler.java:725)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.os.Looper.loop(Looper.java:137)
03-13 21:39:57.381: E/AndroidRuntime(22701): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-13 21:39:57.381: E/AndroidRuntime(22701): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 21:39:57.381: E/AndroidRuntime(22701): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 21:39:57.381: E/AndroidRuntime(22701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-13 21:39:57.381: E/AndroidRuntime(22701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-13 21:39:57.381: E/AndroidRuntime(22701): at dalvik.system.NativeStart.main(Native Method)
我用这个来解决问题:
String selected;
selected = lv.getItemAtPosition(position).toString();
答案 0 :(得分:0)
你必须做以下代码
Intent appInfo = new Intent(MainActivity.this, MapActivity.class);
MainActivity.this.startActivity(appInfo);