我一直在关注Udemy的一些Android开发教程,这些教程非常有帮助,但现在我遇到了导师代码的问题。不确定这是因为更新的SDK还是什么,但代码在他的视频中没有错误。
package com.example.app;
import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]countries=getResources().getStringArray(R.array.countries);
this.setListAdapter(new ArrayAdapter<String>(
this,
R.layout.list_element,
R.id.countryName,
countries));
}
ListView listview = getListView();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View clickView, int position, long id) {
String country = countries[position];
Toast.makeText(MainActivity.this, String.format("%s was chosen.", country),
Toast.LENGTH_SHORT).show();
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
错误从此处开始,出现“无法解析符号”错误。
listview.setOnItemClickListener
并且在整个包含循环中继续出现更多错误。
答案 0 :(得分:7)
在onCreate()中编写代码...你在方法之外编写语法错误......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries);
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_element, R.id.countryName, countries));
ListView listview = getListView();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View clickView,
int position, long id) {
String country = countries[position];
Toast.makeText(MainActivity.this,
String.format("%s was chosen.", country),
Toast.LENGTH_SHORT).show();
}
});
}
答案 1 :(得分:2)
这行代码
listview.setOnItemClickListener
不能直接放在一个类中,它必须放在范围内一些函数,如onCreate()
答案 2 :(得分:2)
不要使用这个:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View clickView, int position, long id)
{
.
. //your code
.
}
}
而是 @Override 扩展类的OnCreate之外的方法 onListItemClick 方法 ListActivity
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
.
. //your code
.
}
答案 3 :(得分:0)
您需要在onCreate()方法中编写该代码。