此适配器类始终正常工作;但是,当我添加启动画面并启动启动画面作为启动器活动时,此适配器类在getView函数上提供nullpointerexception。
MYADAPTER.JAVA
package com.zoopoo.a_man.zoopoo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
Context context;
String[] listItems;
int[] images = {R.drawable.home, R.drawable.notification, R.drawable.gallery, R.drawable.about};
public MyAdapter(Context context) {
this.context = context;
listItems = context.getResources().getStringArray(R.array.list_item);
}
@Override
public int getCount() {
return listItems.length;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_list, viewGroup, false);
} else {
row = viewGroup;
}
ImageView iv = (ImageView) row.findViewById(R.id.iv);
TextView tv = (TextView) row.findViewById(R.id.tv);
tv.setText(listItems[i]); // error comes here..!!
iv.setImageResource(images[i]);
return row;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public Object getItem(int i) {
return listItems[i];
}
}
SPLASH.JAVA
package com.zoopoo.a_man.zoopoo;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.WindowManager;
import android.widget.TextView;
public class Splash extends ActionBarActivity {
TextView tvsplash;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
tvsplash = (TextView)findViewById(R.id.tvsplash);
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/DroidSerif-BoldItalic.ttf");
tvsplash.setTypeface(typeface);
Thread wait = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
Intent register = new Intent("com.zoopoo.a_man.zoopoo.MAINACTIVITY");
startActivity(register);
}
}
};
wait.start();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zoopoo.a_man.zoopoo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".Splash"
android:label="@string/app_name"
android:theme="@style/FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GcmRegister"
android:label="zooPoo"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="com.zoopoo.a_man.zoopoo.GCMREGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">`enter code here`
<!-- Whenever i set this as launcher activity app runs fine, but when i launch this from splash activity, app crashes at MyAdapter's getView. -->
<intent-filter>
<action android:name="com.zoopoo.a_man.zoopoo.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
LOGCAT OUTPUT
07-22 09:20:19.613 5043-5043/com.zoopoo.a_man.zoopoo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.zoopoo.a_man.zoopoo.MyAdapter.getView(MyAdapter.java:40)
at android.widget.AbsListView.obtainView(AbsListView.java:2054)
at android.widget.ListView.makeAndAddView(ListView.java:2004)
at android.widget.ListView.fillSpecific(ListView.java:1550)
at android.widget.ListView.layoutChildren(ListView.java:1835)
at android.widget.AbsListView.onLayout(AbsListView.java:1905)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:914)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11359)
at android.view.ViewGroup.layout(ViewGroup.java:4531)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1665)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
适配器的getView()存在问题。您正在row = viewGroup;
但它应该是row = view;
。如下,
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_list, viewGroup, false);
} else {
row = view; // CHECK THIS LINE!!!!!!
}
view
是转到getView进行回收的convertView实例,如果它不为null则可以重复使用,这就是你的代码的意图!
当您执行row = viewGroup;
,然后尝试row.findViewById(R.id.tv)
时,它会返回null
,当您尝试setText()
时,您会获得NPE!
希望这会有所帮助。 :)