问题描述
- android listview adapter
- lv.setAdapter(new ArrayAdapter(thisandroid.R.layout.simple_list_item_1 strs));
其中lv是一个listview的对象,已获取lv的id,strs是已定义的字符串数组,现在只要添加这句,程序不报错,但是运行错误,删除这句就能运行了
解决方案
把错误发上来把错误发上来
解决方案二:
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;@Overrideprotected 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>(thisandroid.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(thisandroid.R.layout.simple_list_item_1 strs));
this.setContentView(R.layout.gridlayout);
解决方案八:
依旧不对,我现在怀疑是不是android stdio的问题,用OnClickListener也是不报错然后运行错误