我已经设置了listactivity,一切正常,直到我想实现onitemclick()方法。我想在项目点击上启动一个新活动,但它给出了空指针异常。我在清单中宣布了我的所有活动。这是由列表组成的活动:
DBLib.java
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class DBLib extends ListActivity {
private ProgressDialog pDialog;
private EditText inputSearch;
SimpleAdapter adapter;
private static final String READ_DB_URL ="http://crshaggy.byethost7.com/webservice/warehouse.php";
private static final String TAG_TITLE = "title";
private static final String TAG_POSTS = "posts";
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dblib_list);
//inputSearch = (EditText)findViewById(R.id.et_search);
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_DB_URL);
try{
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
String title = c.getString(TAG_TITLE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
void updateList() {
adapter = new SimpleAdapter(this, mCommentList,
R.layout.post, new String[] { TAG_TITLE},
new int[] { R.id.title});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent openPost = new Intent(DBLib.this, BookDetail.class);
startActivity(openPost);
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DBLib.this);
pDialog.setMessage("Loading Warehouse...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
inputSearch = (EditText) findViewById(R.id.et_search);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
DBLib.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
}
这是错误日志
08-06 14:13:45.116: E/AndroidRuntime(1673): FATAL EXCEPTION: main
08-06 14:13:45.116: E/AndroidRuntime(1673): Process: com.example.test, PID: 1673
08-06 14:13:45.116: E/AndroidRuntime(1673): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.BookDetail}: java.lang.NullPointerException
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.os.Handler.dispatchMessage(Handler.java:102)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.os.Looper.loop(Looper.java:136)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 14:13:45.116: E/AndroidRuntime(1673): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 14:13:45.116: E/AndroidRuntime(1673): at java.lang.reflect.Method.invoke(Method.java:515)
08-06 14:13:45.116: E/AndroidRuntime(1673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 14:13:45.116: E/AndroidRuntime(1673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 14:13:45.116: E/AndroidRuntime(1673): at dalvik.system.NativeStart.main(Native Method)
08-06 14:13:45.116: E/AndroidRuntime(1673): Caused by: java.lang.NullPointerException
08-06 14:13:45.116: E/AndroidRuntime(1673): at com.example.test.BookDetail.<init>(BookDetail.java:14)
08-06 14:13:45.116: E/AndroidRuntime(1673): at java.lang.Class.newInstanceImpl(Native Method)
08-06 14:13:45.116: E/AndroidRuntime(1673): at java.lang.Class.newInstance(Class.java:1208)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-06 14:13:45.116: E/AndroidRuntime(1673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
08-06 14:13:45.116: E/AndroidRuntime(1673): ... 11 more
BookDetail.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class BookDetail extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_detail);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<activity
android:name=".JSONParser"
android:label="@string/title_activity_jsonpaerser" >
</activity>
<activity
android:name=".Login"
android:label="@string/title_activity_login" >
<intent-filter>
<action android:name="android.intent.action.LOGIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Register"
android:label="@string/title_activity_register" >
</activity>
<activity
android:name=".StartingPoint"
android:label="@string/title_activity_starting_point" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DBLib"
android:label="@string/title_activity_dblib" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".Index"
android:label="@string/title_activity_index" >
</activity>
<activity
android:name=".BookDetail"
android:label="@string/title_activity_book_detail">
<intent-filter>
<action android:name="android.intent.action.BOOKDETAIL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
试试这个:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(getApplicationContext(), BookDetail.class);
startActivity(intent);
}