我得到了一个简单的ListView的方法,例如:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
但每当我在我的活动中这样做时,我都会得到一个nullpointerexception。
有人可以告诉我它应该是怎样的吗?谢谢!
编辑:对不起!我不想只用代码填写整个问题,我认为在这种情况下它是必要的,所以我把我的整个java代码放在下面:import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Save extends ListActivity {
String FileName;
EditText et;
String listItem[]={};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_save);
Button save = (Button) findViewById(R.id.SAVE); //this is my button
final EditText title = (EditText) findViewById(R.id.save_filename); //This is unused
final EditText maintext = (EditText) findViewById(R.id.test);
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
et = (EditText) findViewById(R.id.save_filename);
listView = (ListView) findViewById(R.id.list); //I've specified this listView only for the OnItemClickListener action
List values = new ArrayList();
for (int i = 0; i < listItem.length; i++) {
values.add(listItem[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileName = et.getText().toString() + ".txt";
FileOutputStream fout = openFileOutput(FileName, Context.MODE_PRIVATE);
OutputStreamWriter outsw = new OutputStreamWriter(fout);
try {
outsw.write(String.valueOf(maintext));
outsw.flush();
outsw.close();
Toast.makeText(getBaseContext(), "Your file has been saved", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
String device;
switch (view.getId()) {
case R.id.SAVE:
List myList = new ArrayList();
device = et.getText().toString();
myList.add(device);
adapter.add(device);
et.setText("");
break;
}
adapter.notifyDataSetChanged();
}
});
//I think this is wrong, I get no error in Java by the way. How could this work if there's a specific extension for my activity that define the listview? **That's my question**.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
和xml:
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/SAVE"
android:text="@string/save_button" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save_filename"
android:layout_alignRight="@+id/linearLayout"
android:hint="@string/save_hint"
android:layout_alignEnd="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp" />
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_above="@+id/linearLayout"
android:layout_below="@+id/save_filename">
</ListView>
对于cunfusion再次抱歉!!
答案 0 :(得分:0)
这里的问题来自于您使用ListActivity而不是Activity。因此,ListView的ID不会添加到R文件中,因此findById调用无法调用它。如果使用Activity,则不会发生这种情况。使用ListActivity时,您可以使用内置的getListView()方法来获取ListView对象。 此外,当我运行您给定的代码时,我得到一个错误,即listView必须由ID列表定义。这来自您定义ID时XML文件中的错误。
下面我发布了您给定XML的两个部分
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save_filename"
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_above="@+id/linearLayout"
android:layout_below="@+id/save_filename">
</ListView>
请注意,在定义ID时,顶部显示android:id="@+id/save_filename"
,而底部显示android:id="@android:id/list". Also when you use save_file_name it is done like this
android:layout_below =&#34; @ + id / save_filename&#34; This is incorrect. To define an id you use
@ + id`。这应该在第一次引用元素时完成。它告诉android在R.java中为它生成一个值。 XML中的错误可能会导致您的R.java文件无法生成并导致不同的问题,但我认为ID的额外优势是可以原谅的。
由于您为布局定义了id,我收到了错误。相反它应该看起来更像这个
android:id="@+id/android:list"
注意+来创建一个新的id,因为我使用的是ListActivity,我还必须将它定义为android:list而不是list。 我将XML更改为:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save_filename"
android:layout_alignRight="@+id/linearLayout"
android:hint="save here"
android:layout_alignEnd="@id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp" />
<ListView
android:id="@+id/android:list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_above="@id/linearLayout"
android:layout_below="@id/save_filename">
</ListView>
这是一些更正的代码,我注意到我改变了什么。
package com.example.sosandbox;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Save extends ListActivity {
String FileName;
EditText et;
//Added some test values
String listItem[]={"1","2","3","4","5"};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_save);
Button save = (Button) findViewById(R.id.SAVE); //this is my button
// This is unused so I commented it out
// final EditText title = (EditText) findViewById(R.id.save_filename); //This is unused
final EditText maintext = (EditText) findViewById(R.id.save_filename);
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
et = (EditText) findViewById(R.id.save_filename);
// This is your issue.
// listView = (ListView) findViewById(R.id.list);
// Change it to this because you are using a listActivity
listView = getListView();//I've specified this listView only for the OnItemClickListener action
List values = new ArrayList();
// This won't run because you initilize listItem to an empty array or 0 so 0 < 0 evaluates to false and the loop doesn't run
for (int i = 0; i < listItem.length; i++) {
values.add(listItem[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileName = et.getText().toString() + ".txt";
FileOutputStream fout = openFileOutput(FileName, Context.MODE_PRIVATE);
OutputStreamWriter outsw = new OutputStreamWriter(fout);
try {
outsw.write(String.valueOf(maintext));
outsw.flush();
outsw.close();
Toast.makeText(getBaseContext(), "Your file has been saved", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
String device;
switch (view.getId()) {
case R.id.SAVE:
List myList = new ArrayList();
device = et.getText().toString();
myList.add(device);
adapter.add(device);
et.setText("");
break;
}
adapter.notifyDataSetChanged();
}
});
//I think this is wrong, I get no error in Java by the way. How could this work if there's a specific extension for my activity that define the listview? **That's my question**.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
}