Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在listview中点击按钮,但它会使应用程序崩溃_Android - Fatal编程技术网

Android 在listview中点击按钮,但它会使应用程序崩溃

Android 在listview中点击按钮,但它会使应用程序崩溃,android,Android,下面是要放入列表中的我的xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF

下面是要放入列表中的我的xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:background="#FFFFFF"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:focusable="false" />

        <TextView
            android:id="@+id/tvCityName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" 
           android:textSize="15dp"
           android:textColor="@drawable/text_hover"


           android:paddingLeft="30dp"
           android:paddingTop="9dp"
            android:textStyle="normal"
           android:background="@drawable/dest_hover"


            />

    </LinearLayout>



</LinearLayout>
日志cat中显示的错误:

08-30 05:43:30.440: E/AndroidRuntime(1442): FATAL EXCEPTION: main
08-30 05:43:30.440: E/AndroidRuntime(1442): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Tab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Destination}: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.os.Looper.loop(Looper.java:137)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at dalvik.system.NativeStart.main(Native Method)
08-30 05:43:30.440: E/AndroidRuntime(1442): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaven.triphills/com.emaven.triphills.Destination}: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.widget.TabHost.addTab(TabHost.java:240)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at com.emaven.triphills.Tab.onCreate(Tab.java:28)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.Activity.performCreate(Activity.java:5104)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-30 05:43:30.440: E/AndroidRuntime(1442):     ... 11 more
08-30 05:43:30.440: E/AndroidRuntime(1442): Caused by: java.lang.NullPointerException
08-30 05:43:30.440: E/AndroidRuntime(1442):     at com.emaven.triphills.Destination.onCreate(Destination.java:51)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.Activity.performCreate(Activity.java:5104)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-30 05:43:30.440: E/AndroidRuntime(1442):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-30 05:43:30.440: E/AndroidRuntime(1442):     ... 21 more

请帮我解决这个问题。谢谢

请在清单文件中的应用程序标记内添加活动类

<activity android:name="com.emaven.triphills.Destination"/>

您正在单击的按钮位于另一个xml文件中,您正在将该文件添加到
列表视图中,但不在作为主布局的
destination.xml中。这就是为什么你会得到这个例外

看看你的代码,我可以给你以下建议

像这样在Listview适配器中获取按钮

View vi = convertView;

Button  chk=(Button)vi.findViewById(R.id.check);  
 chk.setFocusableInTouchMode(false);
            chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.i("sukjhdskfskdgf", "ggogogogogogogogoggo");

                }
            });

很抱歉这么说,但你完全错了。你应该慢慢来,通读一本书

ListView背后的基本思想是使用dataset中的项填充列表。要实现这一点,您需要定义一个带有ListView的活动布局。创建布局以显示单个项目(我怀疑这就是您包含的布局)。 要填充列表视图,必须定义一个适配器,该适配器接受数据集,并为各个项目生成视图

Android平台为带有默认项目视图的字符串数组提供默认列表适配器,但如果您想在每个项目上都有一个按钮,则必须创建自己的布局和适配器

顺便说一句,如果每个项目只有一个操作-检查-关联,那么可以使用简单的itemclicklistener并松开按钮。这样,您就可以使用默认列表适配器


如果检查导致某种选择状态,

日志猫的主要部分缺失。可以重新发布吗?将布局发布到listview所在的位置,并且不需要2行布局。使用单行布局或单行相对布局。我确信按钮不在destination.xml文件中。这就是为什么会出现此异常。是的,兄弟……但我使用的是:simpledapter adapter=new simpledapter(Destination.this,hslist,R.layout.destnationlist,from,to);这是我上面提到的我的目标列表xxl文件,那么我怎样才能得到solution@RavindMaurya您可以
findViewById
将当前视图层次结构设置为活动。您的当前布局位于
destination.xml
中,按钮位于不同的xml中。你不能像这样初始化that@RavindMaurya:正如Raghunandan所说,您不能这样初始化按钮。由于按钮在您的listview中,我编辑了我的帖子。看到了吗。@Raghunandan那么我如何在list@RavindMaurya:如果不使用自定义适配器,恐怕无法实现此目标。请选择自定义适配器,它非常简单可靠。
View vi = convertView;

Button  chk=(Button)vi.findViewById(R.id.check);  
 chk.setFocusableInTouchMode(false);
            chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.i("sukjhdskfskdgf", "ggogogogogogogogoggo");

                }
            });