我正在尝试初始化ListView
,然后我收到错误,说明setListAdapter(ArrayAdapter<String>)
类型的方法SurveyActivity
未定义。我也知道getListView()
未定义。有任何想法吗?代码:
public class SurveyActivity extends Activity implements OnCheckedChangeListener {
CheckBox cb;
String myChoice1;
String myChoice2;
String myChoice3;
String myChoice4;
String myChoice5;
String myChoice6;
String myChoice7;
String myChoice8;
String myChoice9;
String myChoice10;
RadioButton radio1,radio2,radio3,radio4,radio5,radio6,radio7,radio8,radio9,radio10;
RadioGroup rg1,rg2,rg3,rg4,rg5;
EditText text1,text2,text3;
//Button button1;
Button button2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] countrie = getResources().getStringArray(R.array.cars);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countrie));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
cb=(CheckBox)findViewById(R.id.check);
cb.setOnCheckedChangeListener(this);
//RadioGroup rg1=(RadioGroup)findViewById(R.id.rg1);
RadioGroup rg2=(RadioGroup)findViewById(R.id.rg2);
RadioGroup rg3=(RadioGroup)findViewById(R.id.rg3);
RadioGroup rg4=(RadioGroup)findViewById(R.id.rg4);
RadioGroup rg5=(RadioGroup)findViewById(R.id.rg5);
//radio1=(RadioButton)findViewById(R.id.radio1);
// radio2=(RadioButton)findViewById(R.id.radio2);
//radio3=(RadioButton)findViewById(R.id.radio3);
//radio4=(RadioButton)findViewById(R.id.radio4);
//radio5=(RadioButton)findViewById(R.id.radio5);
radio6=(RadioButton)findViewById(R.id.radio6);
radio7=(RadioButton)findViewById(R.id.radio7);
radio8=(RadioButton)findViewById(R.id.radio8);
radio9=(RadioButton)findViewById(R.id.radio9);
radio10=(RadioButton)findViewById(R.id.radio10);
// rg.setOnCheckedChangeListener(this);
/* rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio1:
myChoice2 = "mazda";
//text1.setText(myChoice);
break;
case R.id.radio2:
myChoice2 = "Totota";
//text1.setText(myChoice);
break;
case R.id.radio3:
myChoice2 = "Nissan";
//text1.setText(myChoice);
break;
case R.id.radio4:
myChoice2 = "Hyundai";
//text1.setText(myChoice);
break;
}
}
});
*/
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
String[] countries = getResources().getStringArray(R.array.cars);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
textView.setAdapter(adapter);
myChoice2=textView.getText().toString();
//myChoice2="WORKING";
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.Year_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
Spinner spinner22 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.Wheel_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner22.setAdapter(adapter2);
spinner22.setOnItemSelectedListener(new MyOnItemSelectedListener1());
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio5:
myChoice5 = "1.8";
//text1.setText(myChoice);
break;
case R.id.radio6:
myChoice5 = "2.0";
//text1.setText(myChoice);
break;
case R.id.radio7:
myChoice5 = "2.3";
//text1.setText(myChoice);
break;
}
}
});
rg3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio8:
myChoice6 = "Automatic";
//text1.setText(myChoice);
break;
case R.id.radio9:
myChoice6 = "Manual";
//text1.setText(myChoice);
break;
}
}
});
rg4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio10:
myChoice9 = "Yes";
//text1.setText(myChoice);
break;
case R.id.radio11:
myChoice9 = "No";
//text1.setText(myChoice);
break;
}
}
});
rg5.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId)
{
case R.id.radio12:
myChoice10 = "Yes";
//text1.setText(myChoice);
break;
case R.id.radio13:
myChoice10 = "No";
//text1.setText(myChoice);
break;
}
}
});
//text1.setText(myChoice1);
// button1 = (Button) findViewById(R.id.button01);
// button1.setOnClickListener(new clicker());
button2 = (Button) findViewById(R.id.button02);
button2.setOnClickListener(new clicker());
/* Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
*/
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Toast.makeText(parent.getContext(), "The planet is " +
// parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
myChoice3=parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
public class MyOnItemSelectedListener1 implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Toast.makeText(parent.getContext(), "The planet is " +
// parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
myChoice8=parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if (isChecked)
{
cb.setText("Yes , I have a car!");
myChoice1="I have a car ";
}
else
{
cb.setText(" ");
}
if (isChecked) {
TextView tv= new TextView (this);
tv.setText("You have a car , nice!");
}
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
/* if(v==button1)
{
//text1.setText(myChoice1);
Toast.makeText(getBaseContext(),
"~~~~Successfully submitted AFTER u hit the button below~~~",
Toast.LENGTH_LONG).show();
} */
if(v==button2)
{
text2=(EditText)findViewById(R.id.etext2);
text1=(EditText)findViewById(R.id.etext1);
//text2.setText(text1.getText().toString());
myChoice4=text1.getText().toString();
myChoice7=text2.getText().toString();
Intent viewDataIntent = new Intent(SurveyActivity.this, Survey2.class);
String myData = "You should see this";
viewDataIntent.putExtra("valueOne", myData);
viewDataIntent.putExtra("k1", myChoice1);
viewDataIntent.putExtra("k2", myChoice2);
viewDataIntent.putExtra("k3", myChoice3);
viewDataIntent.putExtra("k4", myChoice4);
viewDataIntent.putExtra("k5", myChoice5);
viewDataIntent.putExtra("k6", myChoice6);
viewDataIntent.putExtra("k7", myChoice7);
viewDataIntent.putExtra("k8", myChoice8);
viewDataIntent.putExtra("k9", myChoice9);
viewDataIntent.putExtra("k10", myChoice10);
startActivity(viewDataIntent);
// Toast.makeText(getBaseContext(),text1.getText(),
// Toast.LENGTH_LONG).show();
}
}
}
}
这是我的Logcat
:
03-01 12:43:07.164: W/dalvikvm(802): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-01 12:43:07.194: E/AndroidRuntime(802): FATAL EXCEPTION: main
03-01 12:43:07.194: E/AndroidRuntime(802): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-01 12:43:07.194: E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 12:43:07.194: E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 12:43:07.194: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-01 12:43:07.194: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-01 12:43:07.194: E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
03-01 12:43:07.194: E/AndroidRuntime(802): Caused by: java.lang.NullPointerException
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
03-01 12:43:07.194: E/AndroidRuntime(802): at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
03-01 12:43:07.194: E/AndroidRuntime(802): ... 11 more
答案 0 :(得分:2)
扩展ListActivity
而不是Activity
。如果您展开ListActivity
并设置内容视图,请执行以下操作:
setContentView(R.layout.main);
确保main.xml
ListView
个@android:id/list
元素的ID为{{1}}。