1

我正在编写一个推特应用程序。我正在为用户名/主题标签创建一个搜索栏,并且在将 XML 生成的视图添加到当前存在的 LinearLayout 时遇到了一些问题。

这是 Search.java

public class Search extends Activity{

String searchType = "username";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    EditText t = (EditText)findViewById(R.id.search_content);
    t.setOnClickListener(onClickSearch);
    t.setWidth(225);

    Button b = (Button)findViewById(R.id.search_button_search);
    b.setOnClickListener(onClickButton);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_search, menu);
    return true;
}

OnClickListener onClickSearch = new OnClickListener(){

    public void onClick(View v) {
        EditText searchContent = (EditText)findViewById(R.id.search_content);
        EditText clickedContent = (EditText)v;
        if((EditText)findViewById(R.id.search_content) == (EditText)v){
            EditText t = (EditText)v;
            t.setText("");
        }

        //EditText searchContent = (EditText)findViewById(R.id.search_content);
        //EditText clickedContent = (EditText)v;

        /*if((Button)v == (Button)findViewById(R.id.search_button_type)){
            EditText t = (EditText)findViewById(R.id.search_content);
            Search.this.searchByUserName(t.getText().toString());
        }

        if((Button)v == (Button)findViewById(R.id.search_button_search)){

        }*/
    }

};

OnClickListener onClickButton = new OnClickListener(){

    public void onClick(View v) {
        //if((Button)v == (Button)findViewById(R.id.search_button_search)){
            EditText t = (EditText)findViewById(R.id.search_content);
            Search s = new Search();
            s.searchByUserName(t.getText().toString());
        //}

        //if((Button)v == (Button)findViewById(R.id.search_button_search)){

        //}
    }

};

public void searchByUserName(String userName){
    //System.out.println("beginning->searchByUserName");
    TwitterHandler handler = new TwitterHandler();
    List<View> views = handler.queryUserName(userName, this);

    LinearLayout ll = (LinearLayout)findViewById(R.id.search_panel);
    for(int i = 0; i < views.size(); i++){
        ll.addView(views.get(i));
    }
}

}

这是XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/search_panel" >
    <Button
       android:id="@+id/search_button_type"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Search Type"/>

    <EditText
        android:id="@+id/search_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search Here" >
    </EditText>

    <Button
        android:id="@+id/search_button_search"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Search"/>
</LinearLayout>

我的 LogCat 告诉我有一个 NullPointerException,进程被迫在 android 虚拟设备上退出。请帮忙!!!

这是 TwitterHandler

public class TwitterHandler extends DefaultHandler{
private final String USER_SEARCH = "https://api.twitter.com/1/users/lookup.xml?screen_name=";

public List<View> queryUserName(String userName, Context context){
    System.out.println("beginning->queryUserName");
    String url = USER_SEARCH + userName;
    //ListView view = new ListView(context);
    List<View> views = new ArrayList<View>();
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        System.out.println("in try statement->queryUserName");
        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(url));
        doc = db.parse(is); 

        NodeList nl = doc.getChildNodes();

        for(int i = 0; i < nl.getLength(); i++){
            Element e = (Element)nl.item(i);
            TextView t = new TextView(context);

            String name = "";
            String screenName = "";
            Bitmap bitmap = null;

            Node n = (Node)e.getChild("name");
            name = n.getTextContent();
            t.setText(name);
            views.add(t);

            t = new TextView(context);
            n = (Node)e.getChild("screen_name");
            screenName = n.getTextContent();
            t.setText(screenName);
            views.add(t);

            n = (Node)e.getChild("profile_image_url");
            bitmap = getBitmap(n.getTextContent());
            ImageView imageView = new ImageView(context);
            imageView.setImageBitmap(bitmap);
            views.add(imageView);

        }

    }catch(Exception e){ }
    return views;
}

private Bitmap getBitmap(String imageUrl) throws IOException{
InputStream inputStream = null;
 URL u = new URL(imageUrl);
 URLConnection conn = u.openConnection();

 try{
      HttpURLConnection httpConn = (HttpURLConnection)conn;
      httpConn.setRequestMethod("GET");
      httpConn.connect();

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
          inputStream = httpConn.getInputStream();
      }
 }
 catch (Exception ex) { }

return BitmapFactory.decodeStream(inputStream);

}

}

这是日志

07-26 17:00:24.523: D/AndroidRuntime(335): Shutting down VM
07-26 17:00:24.523: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
07-26 17:00:24.533: E/AndroidRuntime(335): FATAL EXCEPTION: main
07-26 17:00:24.533: E/AndroidRuntime(335): java.lang.ClassCastException:   java.util.AbstractList$SubAbstractListRandomAccess
07-26 17:00:24.533: E/AndroidRuntime(335):  at    com.example.twitter.viewer.Search.searchByUserName(Search.java:84)
07-26 17:00:24.533: E/AndroidRuntime(335):  at com.example.twitter.viewer.Search$2.onClick(Search.java:71)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.view.View.performClick(View.java:2485)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.view.View$PerformClick.run(View.java:9080)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.os.Handler.handleCallback(Handler.java:587)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)
07-26 17:00:24.533: E/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-26 17:00:24.533: E/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 17:00:24.533: E/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:507)
07-26 17:00:24.533: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-26 17:00:24.533: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-26 17:00:24.533: E/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

You are trying to make a new Instance Object of your Search Activity. In which your contentView is not available, so you can not define LinearLayout at searchByUserName method.

Instead just simple use method name..

So Replace these lines,

Search s = new Search();
s.searchByUserName(t.getText().toString());

with only,

searchByUserName(t.getText().toString());
于 2012-07-26T06:27:20.213 回答