无法启动活动,可能由线程/处理程序/适配器/列表视图引起,不确定

时间:2014-04-14 15:01:27

标签: android multithreading adapter handler

我在Android上写了一个书籍搜索应用程序,当我点击按钮开始搜索时,我的应用程序强制关闭我的代码有问题 - 活动。从日志开始,我想也许我使用了处理程序或线程或适配器不正确,但我无法弄明白。请帮助,谢谢。

活动档案:

package com.example.doubanbook.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

import com.example.doubanbook.R;
import com.example.doubanbook.book.BookSearchInfo;
import com.example.doubanbook.book.PaseSearchReasult;
import com.example.doubanbook.book.Util;

public class SearchBookActivity extends Activity {

    private EditText searchEditText = null;
    private Button searchButton = null;
    private ListView lv = null;
    private Handler hd ;
    private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
    private SimpleAdapter adapt = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchbook);

        searchEditText = (EditText) findViewById(R.id.search_et);
        searchButton = (Button) findViewById(R.id.search_button);
        lv = (ListView) findViewById(R.id.lv);
        searchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String keystr = searchEditText.getText().toString();

                new DownloadThread("https://api.douban.com/v2/book/search?q="
                        + keystr).start();

            }
        });
        hd = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                ArrayList<BookSearchInfo> infos = (ArrayList<BookSearchInfo>) msg.obj;
                for (int i = 0; i < infos.size(); i++) {
                    Map<String, Object> listItem = new HashMap<String, Object>();
                    listItem.put("title", infos.get(i).getTitle());
                    listItem.put("author", infos.get(i).getAuthor());
                    listItem.put("isbn", infos.get(i).getIsbn());
                    listItem.put("publisher", infos.get(i).getPublisher());
                    listItem.put("rate", infos.get(i).getRate());
                    listItem.put("image", infos.get(i).getCover());
                    listItems.add(listItem);
                }
                adapt = new SimpleAdapter(SearchBookActivity.this, listItems,
                        R.layout.search_listitem,
                        new String[] { "title", "author", "isbn", "publisher",
                                "rate", "image" }, new int[] { R.id.tv1,
                                R.id.tv2, R.id.tv3, R.id.tv4, R.id.iv });
                adapt.setViewBinder(new ViewBinder() {

                    @Override
                    public boolean setViewValue(View view, Object data,
                            String textRepresentation) {
                        if ((view instanceof ImageView)
                                & (data instanceof Bitmap)) {
                            ImageView iv = (ImageView) view;
                            Bitmap bm = (Bitmap) data;
                            iv.setImageBitmap(bm);
                            return true;
                        }
                        return false;
                    }
                });
                lv.setAdapter(adapt);
            }

        };
    }
    class DownloadThread extends Thread {
        String url = null;

        public DownloadThread(String urlstr) {
            url = urlstr;
        }

        public void run() {
            String result = Util.Download(url);
            PaseSearchReasult mess = new PaseSearchReasult(result);
            ArrayList<BookSearchInfo> infos = mess.ResultCollection();
            Message msg = Message.obtain();
            msg.obj = infos;

            hd.sendMessage(msg);

        }
    }
}

布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="search" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/search_et"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:background="@null"
                android:gravity="center"
                android:textColor="#ffffff"
                android:hint="type your keyword" />

            <Button
                android:id="@+id/search_btn"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:text="search"
                android:layout_weight="1" />
        </LinearLayout>

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>

</ScrollView>

列出项目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="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lv1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lv2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lv3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lv4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />


</LinearLayout>

记录信息:

04-14 14:33:38.269: E/OpenGLRenderer(1263): Getting MAX_TEXTURE_SIZE from GradienCache
04-14 14:33:38.273: E/OpenGLRenderer(1263): MAX_TEXTURE_SIZE: 16384
04-14 14:33:38.285: E/OpenGLRenderer(1263): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
04-14 14:33:38.285: E/OpenGLRenderer(1263): MAX_TEXTURE_SIZE: 16384
04-14 14:33:43.145: E/AndroidRuntime(1263): FATAL EXCEPTION: main
04-14 14:33:43.145: E/AndroidRuntime(1263): Process: com.example.doubanbook, PID: 1263
04-14 14:33:43.145: E/AndroidRuntime(1263): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.doubanbook/com.example.doubanbook.activity.SearchBookActivity}: java.lang.NullPointerException
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.os.Looper.loop(Looper.java:136)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at java.lang.reflect.Method.invoke(Method.java:515)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at dalvik.system.NativeStart.main(Native Method)
04-14 14:33:43.145: E/AndroidRuntime(1263): Caused by: java.lang.NullPointerException
04-14 14:33:43.145: E/AndroidRuntime(1263):     at com.example.doubanbook.activity.SearchBookActivity.onCreate(SearchBookActivity.java:45)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.Activity.performCreate(Activity.java:5231)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-14 14:33:43.145: E/AndroidRuntime(1263):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-14 14:33:43.145: E/AndroidRuntime(1263):     ... 11 more

2 个答案:

答案 0 :(得分:1)

这里的ID不对:

searchButton = (Button) findViewById(R.id.search_button);

因为在你的layout.xml中你输入了一个稍微不同的id:

android:id="@+id/search_btn"

==&GT;只需在布局和活动中使用相同的 ID并且应该可以正常工作,否则findViewById将返回null,因为它无法找到id。

答案 1 :(得分:1)

SearchBookActivity.java

中第45行有一个NullPointerException

您可以从堆栈跟踪的这一行告诉:

04-14 14:33:43.145: E/AndroidRuntime(1263): Caused by: java.lang.NullPointerException
04-14 14:33:43.145: E/AndroidRuntime(1263):     at com.example.doubanbook.activity.SearchBookActivity.onCreate(SearchBookActivity.java:45)

当您调用方法或引用null对象的成员时,会发生这种情况。

如果您的代码直接映射到您的代码行,则错误的来源很可能就在此行中:

    searchButton.setOnClickListener(new OnClickListener() {

您正在使用findById进行R.id.search_et - 但也许您打算使用R.id.search_btn