android listview adapter

lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, strs));
其中lv是一个listview的对象,已获取lv的id,strs是已定义的字符串数组,现在只要添加这句,程序不报错,但是运行错误,删除这句就能运行了

8个回答

把错误发上来把错误发上来

12-28 21:25:58.529 938-938/com.example.administrator.testview E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3633)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3628)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.administrator.testview.MainActivity.button4OnClick(MainActivity.java:45)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3628)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

你点击动作是怎么处理的

我没有做点击事件,现在就是想实现listview中显示数据而已……

你把代码截图贴出来。。。

public class MainActivity extends ActionBarActivity {

public static Button bt;
public static TextView tv;
public static ListView lv;
private static final String[] strs = new String[] {
    "first", "second", "third", "fourth", "fifth"
};
public static int screenWidth;
public static int screenHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    screenWidth = dm.widthPixels;
    screenHeight = dm.heightPixels;
    setContentView(R.layout.activity_main);
}

public void button4OnClick(View view){
    lv = (ListView)findViewById(R.id.listView);
    //lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strs));
    this.setContentView(R.layout.gridlayout);
}
    我现在只是在熟悉android,新手啊,求详细指教

lv = (ListView)findViewById(R.id.listView); // 是因为这里还没有加载布局,查找到的Lv为null,应该将下面setContentView写在前面
lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, strs));
this.setContentView(R.layout.gridlayout);

依旧不对,我现在怀疑是不是android stdio的问题,用OnClickListener也是不报错然后运行错误

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问