自定义列表视图在 android 中不起作用
custom listview doesn't work in android
嗨,我在 android 中创建了一个应用程序。
我从互联网接收数据,但当我想在自定义列表视图中显示数据时,我的应用程序崩溃并关闭。当我评论这一行时:list.setAdapter(adapter);
我的应用有效
自定义适配器class:
public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListNews tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
activity = a;
data=d;
res = resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
/*********** Depends upon data size called for each row , Create each ListView row ***********/
@SuppressLint("InflateParams") public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.tabitem, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListNews) data.get(position);
holder.text.setText(tempValues.getMenu_name());
holder.text1.setText(tempValues.getPage_title());
holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+tempValues.getImage(),null,null));
vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.onItemClick(mPosition);
}
}
}
列表新闻class
public class ListNews {
String language_id;
String page_key;
String page_title;
String page_text;
int menu_id;
String menu_link;
String menu_code;
String menu_name;
String ru = "";
String Image = "";
public String getLanguage_id() {
return language_id;
}
public void setLanguage_id(String language_id) {
this.language_id = language_id;
}
public String getPage_key() {
return page_key;
}
public void setPage_key(String page_key) {
this.page_key = page_key;
}
public String getPage_title() {
return page_title;
}
public void setPage_title(String page_title) {
this.page_title = page_title;
}
public String getPage_text() {
return page_text;
}
public void setPage_text(String page_text) {
this.page_text = page_text;
}
public int getMenu_id() {
return menu_id;
}
public void setMenu_id(int menu_id) {
this.menu_id = menu_id;
}
public String getMenu_link() {
return menu_link;
}
public void setMenu_link(String menu_link) {
this.menu_link = menu_link;
}
public String getMenu_code() {
return menu_code;
}
public void setMenu_code(String menu_code) {
this.menu_code = menu_code;
}
public String getMenu_name() {
return menu_name;
}
public void setMenu_name(String menu_name) {
this.menu_name = menu_name;
}
public String getRu() {
return ru;
}
public void setRu(String ru) {
this.ru = ru;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
login.java // 用于在列表视图中显示数据
package com.androidexample.customlistview;
import java.io.File;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.StrictMode;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;
public class Login extends Activity {
Context context = Login.this;
ListView list;
CustomAdapter adapter;
public Login CustomListView = null;
public ArrayList<ListNews> gettabledata,getdatabasedata;
SQLiteDatabase db;
Boolean b;
Resources res;
ArrayList<News> newsha;
ArrayList<String> language_id, page_key, page_title, page_text, menu_id,
menu_link, menu_code, menu_name, ru, im;
JSONObject json_data;
JSONArray jArray;
DbGetData dbGetData3;
Boolean boolean1;
ContextWrapper wrapper = Login.this;
News n;
String data, url, msg, check, lan;
ConnectivityManager conMan;
State mobile, wifi;
TelephonyManager telephonyManager;
int sizearray = 0, sizetable = 0, sizedb = 0;;
ListNews td;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list_view_android_example);
StrictMode.enableDefaults();
CustomListView = Login.this;
set();
array();
checkconnect();
getdatafromdb();
adapter = new CustomAdapter(CustomListView, gettabledata, res);
list.setAdapter(adapter);
} // on create
private void set() {
try {
boolean1 = doesDatabaseExist(wrapper, "test.db");
data = "";
url = "";
gettabledata = new ArrayList<ListNews>();
getdatabasedata = new ArrayList<ListNews>();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
res = getResources();
list = (ListView) findViewById(R.id.list5);
data = "";
url = "";
} catch (Exception e) {
}
} // end set
private static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
private void array() {
try {
language_id = new ArrayList<String>();
page_key = new ArrayList<String>();
page_title = new ArrayList<String>();
page_text = new ArrayList<String>();
menu_id = new ArrayList<String>();
menu_link = new ArrayList<String>();
menu_code = new ArrayList<String>();
menu_name = new ArrayList<String>();
ru = new ArrayList<String>();
im = new ArrayList<String>();
language_id.clear();
page_key.clear();
page_title.clear();
page_text.clear();
menu_id.clear();
menu_link.clear();
menu_code.clear();
menu_name.clear();
ru.clear();
im.clear();
}
catch (Exception e)
{
}
}
private void getdatafromdb() {
try {
url = "my url address";
dbGetData3 = new DbGetData();
new Thread(new Runnable() {
public void run() {
data = dbGetData3.getDataFromDB(url);
runOnUiThread(new Runnable() {
@Override
public void run() {
newsha = parseJSON3(data);
}
});
}
}).start();
} catch (Exception e) {
}
}
public ArrayList<News> parseJSON3(String result) {
ArrayList<News> userha = new ArrayList<News>();
try {
jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
n = new News();
n.setLanguage_id(json_data.getString("language_id"));
language_id.add(json_data.getString("language_id"));
n.setPage_key(json_data.getString("page_key"));
page_key.add(json_data.getString("page_key"));
n.setPage_title(json_data.getString("page_title"));
page_title.add(json_data.getString("page_title"));
n.setPage_text(json_data.getString("page_text"));
page_text.add(json_data.getString("page_text"));
n.setMenu_id(json_data.getInt("menu_id"));
menu_id.add(String.valueOf(json_data.getInt("menu_id")));
n.setMenu_link(json_data.getString("menu_link"));
menu_link.add(json_data.getString("menu_link"));
n.setMenu_code(json_data.getString("menu_code"));
menu_code.add(json_data.getString("menu_code"));
n.setMenu_name(json_data.getString("menu_name"));
menu_name.add(json_data.getString("menu_name"));
ru.add("ak3");
im.add("image5");
userha.add(n);
}
sizearray = language_id.size();
if (sizearray <= 0) {
getdatafromdb();
} else {
sizedb = sizearray;
}
if (boolean1) {
Toast.makeText(context, "true",Toast.LENGTH_SHORT).show();
getFromTable();
if (sizedb != sizetable) {
Toast.makeText(context, "size is not eqal",Toast.LENGTH_SHORT).show();
context.deleteDatabase("test.db");
createTable();
}
else {
Toast.makeText(context, "size is eqal",Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "false",Toast.LENGTH_SHORT).show();
createTable();
}
} catch (JSONException e) {
}
return userha;
}
private void getFromTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("select * from tb", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
td = new ListNews();
td.setLanguage_id(cursor.getString(cursor.getColumnIndex("language_id")));
td.setPage_key(cursor.getString(cursor.getColumnIndex("page_key")));
td.setPage_title(cursor.getString(cursor.getColumnIndex("page_title")));
td.setPage_text(cursor.getString(cursor.getColumnIndex("page_text")));
td.setMenu_id(cursor.getInt(cursor.getColumnIndex("menu_id")));
td.setMenu_link(cursor.getString(cursor.getColumnIndex("menu_link")));
td.setMenu_code(cursor.getString(cursor.getColumnIndex("menu_code")));
td.setMenu_name(cursor.getString(cursor.getColumnIndex("menu_name")));
td.setRu(cursor.getString(cursor.getColumnIndex("ru")));
td.setImage(cursor.getString(cursor.getColumnIndex("image")));
getdatabasedata.add(td);
} while (cursor.moveToNext());
Toast.makeText(CustomListView, "get data from table.......", Toast.LENGTH_LONG).show();
}
cursor.close();
db.close();
sizetable = getdatabasedata.size();
Toast.makeText(context, "Database size: " + String.valueOf(sizetable),
Toast.LENGTH_SHORT).show();
}
private void createTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
sizetable = 0;
getdatabasedata.clear();
db.execSQL("create table if not exists tb ("
+ "language_id NVARCHAR NOT NULL,"
+ "page_key NVARCHAR NOT NULL,"
+ "page_title NVARCHAR NOT NULL,"
+ "page_text NVARCHAR NOT NULL,"
+ "menu_id NVARCHAR NOT NULL,"
+ "menu_link NVARCHAR NOT NULL,"
+ "menu_code NVARCHAR NOT NULL,"
+ "menu_name NVARCHAR NOT NULL,"
+ "ru NVARCHAR NOT NULL,"
+ "image NVARCHAR NOT NULL);");
Toast.makeText(context, "Table created.....", Toast.LENGTH_SHORT).show();
db.close();
addToTable();
}
private void addToTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
for (int i = 0; i < sizearray; i++) {
ContentValues values = new ContentValues();
values.put("language_id", language_id.get(i));
values.put("page_key", page_key.get(i));
values.put("page_title", page_title.get(i));
values.put("page_text", page_text.get(i));
values.put("menu_id", menu_id.get(i));
values.put("menu_link", menu_link.get(i));
values.put("menu_code", menu_code.get(i));
values.put("menu_name", menu_name.get(i));
values.put("ru", ru.get(i));
values.put("image", im.get(i));
db.insert("tb", null, values);
}
db.close();
getFromTable();
}
public void onItemClick(int mPosition) {
ListNews tempValues = (ListNews) gettabledata.get(mPosition);
Toast.makeText(
CustomListView,
"" + tempValues.getMenu_name() + " \nImage: "
+ tempValues.getPage_title() + " \nUrl: "
+ tempValues.getPage_text(), Toast.LENGTH_LONG).show();
}
private void checkconnect() {
try {
conMan = (ConnectivityManager) getSystemService(Login.CONNECTIVITY_SERVICE);
mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (mobile == NetworkInfo.State.CONNECTED
|| mobile == NetworkInfo.State.CONNECTING) {
} else if (wifi == NetworkInfo.State.CONNECTED
|| wifi == NetworkInfo.State.CONNECTING) {
} else {
// Login.this.finish();
}
} catch (Exception e) {
}
}
}
login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_gradient" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
tabitem // 列表视图项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="@drawable/bar_bg"
android:paddingTop="0dip" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow android:layout_marginTop="3dp" >
<LinearLayout
android:layout_width="120dp"
android:layout_height="82dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="left|top"
android:scaleType="centerCrop" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="0dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:textDirection="rtl" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="9dp"
android:layout_span="1"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
<TableRow android:layout_width="fill_parent" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:gravity="left"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/ivru"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignBottom="@+id/tableLayout1"
android:layout_alignParentRight="true"
android:layout_marginBottom="19dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:scaleType="centerCrop" />
</RelativeLayout>
记录错误
08-11 01:09:05.267: E/InputDispatcher(1720): Motion event has invalid pointer count 0; value must be between 1 and 16.
08-11 01:09:05.357: E/InputDispatcher(1720): Motion event has invalid pointer count 0; value must be between 1 and 16.
08-11 01:09:05.567: E/AndroidRuntime(4456): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.577: E/AndroidRuntime(4456): FATAL EXCEPTION: main
08-11 01:09:05.577: E/AndroidRuntime(4456): Process: com.androidexample.customlistview, PID: 4456
08-11 01:09:05.577: E/AndroidRuntime(4456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.577: E/AndroidRuntime(4456): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.577: E/AndroidRuntime(4456): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.577: E/AndroidRuntime(4456): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.577: E/AndroidRuntime(4456): Caused by: java.lang.NullPointerException
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.577: E/AndroidRuntime(4456): ... 11 more
08-11 01:09:05.677: E/AndroidRuntime(4472): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.677: E/AndroidRuntime(4472): FATAL EXCEPTION: main
08-11 01:09:05.677: E/AndroidRuntime(4472): Process: com.androidexample.customlistview, PID: 4472
08-11 01:09:05.677: E/AndroidRuntime(4472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.677: E/AndroidRuntime(4472): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.677: E/AndroidRuntime(4472): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.677: E/AndroidRuntime(4472): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.677: E/AndroidRuntime(4472): Caused by: java.lang.NullPointerException
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.677: E/AndroidRuntime(4472): ... 11 more
08-11 01:09:05.757: E/AndroidRuntime(4486): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.787: E/AndroidRuntime(4486): FATAL EXCEPTION: main
08-11 01:09:05.787: E/AndroidRuntime(4486): Process: com.androidexample.customlistview, PID: 4486
08-11 01:09:05.787: E/AndroidRuntime(4486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.787: E/AndroidRuntime(4486): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.787: E/AndroidRuntime(4486): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.787: E/AndroidRuntime(4486): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.787: E/AndroidRuntime(4486): Caused by: java.lang.NullPointerException
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.787: E/AndroidRuntime(4486): ... 11 more
08-11 01:09:47.127: E/cutils-trace(4503): Error opening trace file: No such file or directory (2)
08-11 01:09:47.137: E/memtrack(4503): Couldn't load memtrack module (No such file or directory)
08-11 01:09:47.137: E/android.os.Debug(4503): failed to load memtrack module: -2
您在 login.xml 中创建了 list5
,但您正在使用 activity_custom_list_view_android_example
作为 activity 视图。所以当你使用列表对象时你会得到 NPE
。您必须将 activity_custom_list_view_android_example
更改为 login
即 setContentView(R.layout.login);
像下面这样更改你的 onCreate()
ListView mListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
StrictMode.enableDefaults();
mListView=(ListView)findViewById(R.id.list5)
set();
array();
checkconnect();
getdatafromdb();
if(gettabledata!=null)
{
adapter = new CustomAdapter(CustomListView, gettabledata, res);
}
else
{
adapter = null;
}
adapter = new CustomAdapter(CustomListView, gettabledata, res);
mListView.setAdapter(adapter);
}
嗨,我在 android 中创建了一个应用程序。
我从互联网接收数据,但当我想在自定义列表视图中显示数据时,我的应用程序崩溃并关闭。当我评论这一行时:list.setAdapter(adapter);
我的应用有效
自定义适配器class:
public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListNews tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
activity = a;
data=d;
res = resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
/*********** Depends upon data size called for each row , Create each ListView row ***********/
@SuppressLint("InflateParams") public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.tabitem, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListNews) data.get(position);
holder.text.setText(tempValues.getMenu_name());
holder.text1.setText(tempValues.getPage_title());
holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+tempValues.getImage(),null,null));
vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.onItemClick(mPosition);
}
}
}
列表新闻class
public class ListNews {
String language_id;
String page_key;
String page_title;
String page_text;
int menu_id;
String menu_link;
String menu_code;
String menu_name;
String ru = "";
String Image = "";
public String getLanguage_id() {
return language_id;
}
public void setLanguage_id(String language_id) {
this.language_id = language_id;
}
public String getPage_key() {
return page_key;
}
public void setPage_key(String page_key) {
this.page_key = page_key;
}
public String getPage_title() {
return page_title;
}
public void setPage_title(String page_title) {
this.page_title = page_title;
}
public String getPage_text() {
return page_text;
}
public void setPage_text(String page_text) {
this.page_text = page_text;
}
public int getMenu_id() {
return menu_id;
}
public void setMenu_id(int menu_id) {
this.menu_id = menu_id;
}
public String getMenu_link() {
return menu_link;
}
public void setMenu_link(String menu_link) {
this.menu_link = menu_link;
}
public String getMenu_code() {
return menu_code;
}
public void setMenu_code(String menu_code) {
this.menu_code = menu_code;
}
public String getMenu_name() {
return menu_name;
}
public void setMenu_name(String menu_name) {
this.menu_name = menu_name;
}
public String getRu() {
return ru;
}
public void setRu(String ru) {
this.ru = ru;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
login.java // 用于在列表视图中显示数据
package com.androidexample.customlistview;
import java.io.File;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.StrictMode;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.Toast;
public class Login extends Activity {
Context context = Login.this;
ListView list;
CustomAdapter adapter;
public Login CustomListView = null;
public ArrayList<ListNews> gettabledata,getdatabasedata;
SQLiteDatabase db;
Boolean b;
Resources res;
ArrayList<News> newsha;
ArrayList<String> language_id, page_key, page_title, page_text, menu_id,
menu_link, menu_code, menu_name, ru, im;
JSONObject json_data;
JSONArray jArray;
DbGetData dbGetData3;
Boolean boolean1;
ContextWrapper wrapper = Login.this;
News n;
String data, url, msg, check, lan;
ConnectivityManager conMan;
State mobile, wifi;
TelephonyManager telephonyManager;
int sizearray = 0, sizetable = 0, sizedb = 0;;
ListNews td;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list_view_android_example);
StrictMode.enableDefaults();
CustomListView = Login.this;
set();
array();
checkconnect();
getdatafromdb();
adapter = new CustomAdapter(CustomListView, gettabledata, res);
list.setAdapter(adapter);
} // on create
private void set() {
try {
boolean1 = doesDatabaseExist(wrapper, "test.db");
data = "";
url = "";
gettabledata = new ArrayList<ListNews>();
getdatabasedata = new ArrayList<ListNews>();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
res = getResources();
list = (ListView) findViewById(R.id.list5);
data = "";
url = "";
} catch (Exception e) {
}
} // end set
private static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
private void array() {
try {
language_id = new ArrayList<String>();
page_key = new ArrayList<String>();
page_title = new ArrayList<String>();
page_text = new ArrayList<String>();
menu_id = new ArrayList<String>();
menu_link = new ArrayList<String>();
menu_code = new ArrayList<String>();
menu_name = new ArrayList<String>();
ru = new ArrayList<String>();
im = new ArrayList<String>();
language_id.clear();
page_key.clear();
page_title.clear();
page_text.clear();
menu_id.clear();
menu_link.clear();
menu_code.clear();
menu_name.clear();
ru.clear();
im.clear();
}
catch (Exception e)
{
}
}
private void getdatafromdb() {
try {
url = "my url address";
dbGetData3 = new DbGetData();
new Thread(new Runnable() {
public void run() {
data = dbGetData3.getDataFromDB(url);
runOnUiThread(new Runnable() {
@Override
public void run() {
newsha = parseJSON3(data);
}
});
}
}).start();
} catch (Exception e) {
}
}
public ArrayList<News> parseJSON3(String result) {
ArrayList<News> userha = new ArrayList<News>();
try {
jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
n = new News();
n.setLanguage_id(json_data.getString("language_id"));
language_id.add(json_data.getString("language_id"));
n.setPage_key(json_data.getString("page_key"));
page_key.add(json_data.getString("page_key"));
n.setPage_title(json_data.getString("page_title"));
page_title.add(json_data.getString("page_title"));
n.setPage_text(json_data.getString("page_text"));
page_text.add(json_data.getString("page_text"));
n.setMenu_id(json_data.getInt("menu_id"));
menu_id.add(String.valueOf(json_data.getInt("menu_id")));
n.setMenu_link(json_data.getString("menu_link"));
menu_link.add(json_data.getString("menu_link"));
n.setMenu_code(json_data.getString("menu_code"));
menu_code.add(json_data.getString("menu_code"));
n.setMenu_name(json_data.getString("menu_name"));
menu_name.add(json_data.getString("menu_name"));
ru.add("ak3");
im.add("image5");
userha.add(n);
}
sizearray = language_id.size();
if (sizearray <= 0) {
getdatafromdb();
} else {
sizedb = sizearray;
}
if (boolean1) {
Toast.makeText(context, "true",Toast.LENGTH_SHORT).show();
getFromTable();
if (sizedb != sizetable) {
Toast.makeText(context, "size is not eqal",Toast.LENGTH_SHORT).show();
context.deleteDatabase("test.db");
createTable();
}
else {
Toast.makeText(context, "size is eqal",Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "false",Toast.LENGTH_SHORT).show();
createTable();
}
} catch (JSONException e) {
}
return userha;
}
private void getFromTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("select * from tb", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
td = new ListNews();
td.setLanguage_id(cursor.getString(cursor.getColumnIndex("language_id")));
td.setPage_key(cursor.getString(cursor.getColumnIndex("page_key")));
td.setPage_title(cursor.getString(cursor.getColumnIndex("page_title")));
td.setPage_text(cursor.getString(cursor.getColumnIndex("page_text")));
td.setMenu_id(cursor.getInt(cursor.getColumnIndex("menu_id")));
td.setMenu_link(cursor.getString(cursor.getColumnIndex("menu_link")));
td.setMenu_code(cursor.getString(cursor.getColumnIndex("menu_code")));
td.setMenu_name(cursor.getString(cursor.getColumnIndex("menu_name")));
td.setRu(cursor.getString(cursor.getColumnIndex("ru")));
td.setImage(cursor.getString(cursor.getColumnIndex("image")));
getdatabasedata.add(td);
} while (cursor.moveToNext());
Toast.makeText(CustomListView, "get data from table.......", Toast.LENGTH_LONG).show();
}
cursor.close();
db.close();
sizetable = getdatabasedata.size();
Toast.makeText(context, "Database size: " + String.valueOf(sizetable),
Toast.LENGTH_SHORT).show();
}
private void createTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
sizetable = 0;
getdatabasedata.clear();
db.execSQL("create table if not exists tb ("
+ "language_id NVARCHAR NOT NULL,"
+ "page_key NVARCHAR NOT NULL,"
+ "page_title NVARCHAR NOT NULL,"
+ "page_text NVARCHAR NOT NULL,"
+ "menu_id NVARCHAR NOT NULL,"
+ "menu_link NVARCHAR NOT NULL,"
+ "menu_code NVARCHAR NOT NULL,"
+ "menu_name NVARCHAR NOT NULL,"
+ "ru NVARCHAR NOT NULL,"
+ "image NVARCHAR NOT NULL);");
Toast.makeText(context, "Table created.....", Toast.LENGTH_SHORT).show();
db.close();
addToTable();
}
private void addToTable() {
db = this.openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
for (int i = 0; i < sizearray; i++) {
ContentValues values = new ContentValues();
values.put("language_id", language_id.get(i));
values.put("page_key", page_key.get(i));
values.put("page_title", page_title.get(i));
values.put("page_text", page_text.get(i));
values.put("menu_id", menu_id.get(i));
values.put("menu_link", menu_link.get(i));
values.put("menu_code", menu_code.get(i));
values.put("menu_name", menu_name.get(i));
values.put("ru", ru.get(i));
values.put("image", im.get(i));
db.insert("tb", null, values);
}
db.close();
getFromTable();
}
public void onItemClick(int mPosition) {
ListNews tempValues = (ListNews) gettabledata.get(mPosition);
Toast.makeText(
CustomListView,
"" + tempValues.getMenu_name() + " \nImage: "
+ tempValues.getPage_title() + " \nUrl: "
+ tempValues.getPage_text(), Toast.LENGTH_LONG).show();
}
private void checkconnect() {
try {
conMan = (ConnectivityManager) getSystemService(Login.CONNECTIVITY_SERVICE);
mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (mobile == NetworkInfo.State.CONNECTED
|| mobile == NetworkInfo.State.CONNECTING) {
} else if (wifi == NetworkInfo.State.CONNECTED
|| wifi == NetworkInfo.State.CONNECTING) {
} else {
// Login.this.finish();
}
} catch (Exception e) {
}
}
}
login.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_gradient" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
tabitem // 列表视图项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="@drawable/bar_bg"
android:paddingTop="0dip" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow android:layout_marginTop="3dp" >
<LinearLayout
android:layout_width="120dp"
android:layout_height="82dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="left|top"
android:scaleType="centerCrop" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="0dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:textDirection="rtl" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="9dp"
android:layout_span="1"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
<TableRow android:layout_width="fill_parent" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:gravity="left"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/ivru"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignBottom="@+id/tableLayout1"
android:layout_alignParentRight="true"
android:layout_marginBottom="19dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:scaleType="centerCrop" />
</RelativeLayout>
记录错误
08-11 01:09:05.267: E/InputDispatcher(1720): Motion event has invalid pointer count 0; value must be between 1 and 16.
08-11 01:09:05.357: E/InputDispatcher(1720): Motion event has invalid pointer count 0; value must be between 1 and 16.
08-11 01:09:05.567: E/AndroidRuntime(4456): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.577: E/AndroidRuntime(4456): FATAL EXCEPTION: main
08-11 01:09:05.577: E/AndroidRuntime(4456): Process: com.androidexample.customlistview, PID: 4456
08-11 01:09:05.577: E/AndroidRuntime(4456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.577: E/AndroidRuntime(4456): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.577: E/AndroidRuntime(4456): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.577: E/AndroidRuntime(4456): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.577: E/AndroidRuntime(4456): Caused by: java.lang.NullPointerException
08-11 01:09:05.577: E/AndroidRuntime(4456): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.577: E/AndroidRuntime(4456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.577: E/AndroidRuntime(4456): ... 11 more
08-11 01:09:05.677: E/AndroidRuntime(4472): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.677: E/AndroidRuntime(4472): FATAL EXCEPTION: main
08-11 01:09:05.677: E/AndroidRuntime(4472): Process: com.androidexample.customlistview, PID: 4472
08-11 01:09:05.677: E/AndroidRuntime(4472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.677: E/AndroidRuntime(4472): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.677: E/AndroidRuntime(4472): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.677: E/AndroidRuntime(4472): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.677: E/AndroidRuntime(4472): Caused by: java.lang.NullPointerException
08-11 01:09:05.677: E/AndroidRuntime(4472): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.677: E/AndroidRuntime(4472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.677: E/AndroidRuntime(4472): ... 11 more
08-11 01:09:05.757: E/AndroidRuntime(4486): in writeCrashedAppName, pkgName :com.androidexample.customlistview
08-11 01:09:05.787: E/AndroidRuntime(4486): FATAL EXCEPTION: main
08-11 01:09:05.787: E/AndroidRuntime(4486): Process: com.androidexample.customlistview, PID: 4486
08-11 01:09:05.787: E/AndroidRuntime(4486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.customlistview/com.androidexample.customlistview.Login}: java.lang.NullPointerException
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.access0(ActivityThread.java:135)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.os.Looper.loop(Looper.java:136)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-11 01:09:05.787: E/AndroidRuntime(4486): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 01:09:05.787: E/AndroidRuntime(4486): at java.lang.reflect.Method.invoke(Method.java:515)
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
08-11 01:09:05.787: E/AndroidRuntime(4486): at dalvik.system.NativeStart.main(Native Method)
08-11 01:09:05.787: E/AndroidRuntime(4486): Caused by: java.lang.NullPointerException
08-11 01:09:05.787: E/AndroidRuntime(4486): at com.androidexample.customlistview.Login.onCreate(Login.java:72)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.Activity.performCreate(Activity.java:5231)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
08-11 01:09:05.787: E/AndroidRuntime(4486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 01:09:05.787: E/AndroidRuntime(4486): ... 11 more
08-11 01:09:47.127: E/cutils-trace(4503): Error opening trace file: No such file or directory (2)
08-11 01:09:47.137: E/memtrack(4503): Couldn't load memtrack module (No such file or directory)
08-11 01:09:47.137: E/android.os.Debug(4503): failed to load memtrack module: -2
您在 login.xml 中创建了 list5
,但您正在使用 activity_custom_list_view_android_example
作为 activity 视图。所以当你使用列表对象时你会得到 NPE
。您必须将 activity_custom_list_view_android_example
更改为 login
即 setContentView(R.layout.login);
像下面这样更改你的 onCreate()
ListView mListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
StrictMode.enableDefaults();
mListView=(ListView)findViewById(R.id.list5)
set();
array();
checkconnect();
getdatafromdb();
if(gettabledata!=null)
{
adapter = new CustomAdapter(CustomListView, gettabledata, res);
}
else
{
adapter = null;
}
adapter = new CustomAdapter(CustomListView, gettabledata, res);
mListView.setAdapter(adapter);
}