Java 自定义适配器处的NullPointerException';在我添加启动屏幕后,s getView
这个适配器类一直运行良好;然而,当我添加一个启动屏幕并将启动屏幕作为启动程序活动启动时,这个适配器类在getView函数上给出了一个nullpointerexception MYADAPTER.JAVAJava 自定义适配器处的NullPointerException';在我添加启动屏幕后,s getView,java,android,Java,Android,这个适配器类一直运行良好;然而,当我添加一个启动屏幕并将启动屏幕作为启动程序活动启动时,这个适配器类在getView函数上给出了一个nullpointerexception MYADAPTER.JAVA package com.zoopoo.a_man.zoopoo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import an
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>
适配器的getView()中存在问题。您正在执行
row=viewGroup代码>但它应该是行=视图代码>。如下:
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!!!!!!
}
视图
是到getView循环使用的convertView实例,如果它不为null,则可以重复使用,这就是代码的意图
当您执行row=viewGroup时
,然后尝试row.findViewById(R.id.tv)
,它将返回null
,当您尝试setText()
时,您将获得NPE
希望这有帮助。:) 给我们看看你的堆栈跟踪你能给我们看看你的主要活动吗?你开始活动的方式正确吗。。。这奏效了。。谢谢你,伙计我明白你的解释了!!:-)
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!!!!!!
}