我正在编写推特应用程序。我正在为usernames / hashtags创建一个搜索栏,我在将从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);
}
}
这是Log
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)
答案 0 :(得分:0)
您正在尝试制作Search Activity
的新实例对象。
您的contentView
不可用,因此您无法使用LinearLayout
方法定义searchByUserName
。
而只是简单的使用方法名称..
所以替换这些行,
Search s = new Search();
s.searchByUserName(t.getText().toString());
仅限,
searchByUserName(t.getText().toString());