适配器总是出现空指针问题

Android 码拜 9年前 (2016-06-08) 1115次浏览
问题描述:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.myapplication14/com.example.administrator.myapplication14.Activity.FAdoptActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5407)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.LayoutInflater.from(LayoutInflater.java:210)
at com.example.administrator.myapplication14.Adapter.FAdoptAdapter.<init>(FAdoptAdapter.java:27)
at com.example.administrator.myapplication14.Activity.FAdoptActivity.initAdapter(FAdoptActivity.java:83)
at com.example.administrator.myapplication14.Activity.FAdoptActivity.onCreate(FAdoptActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5234)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
Activity:
package com.example.administrator.myapplication14.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.administrator.myapplication14.Adapter.FAdoptAdapter;
import com.example.administrator.myapplication14.R;
import com.example.administrator.myapplication14.be.FAdoptImage;
import java.util.ArrayList;
public class FAdoptActivity extends AppCompatActivity {
Toolbar toolbar;
ListView listView;
ArrayList<FAdoptImage> arrayList = new ArrayList();
FAdoptAdapter adapter;
//View v;
Context context;
public FAdoptActivity(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadopt2);
Log.i(“–oncreat–“, “onCreate: “);
initData();
Log.i(“– initData();–“, “onCreate: “);
initAdapter();
Log.i(“– initAdapter();–“, “onCreate: “);
initToolbar();
Log.i(“–initToolbar();–“, “onCreate: “);
}
private void initToolbar() {
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar1);
//设置导航栏图标
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
//设置app logo
//toolbar.setLogo(R.mipmap.ic_launcher);
//设置主标题
toolbar.setTitle(“领养”);
//设置右上角的填充菜单
toolbar.inflateMenu(R.menu.fadopt_item);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int menuItemId = item.getItemId();
if (menuItemId == R.id.action_search) {
Toast.makeText(context, R.string.menu_search, Toast.LENGTH_LONG).show();
} else if (menuItemId == R.id.action_notification) {
Toast.makeText(context, R.string.menu_notifications, Toast.LENGTH_LONG).show();
} else if (menuItemId == R.id.action_item1) {
Toast.makeText(context, R.string.item_01, Toast.LENGTH_LONG).show();
} else if (menuItemId == R.id.action_item2) {
Toast.makeText(context, R.string.item_02, Toast.LENGTH_LONG).show();
}
return true;
}
});
}
private void initAdapter() {
adapter=new FAdoptAdapter(context,arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
private void initData() {
toolbar= (Toolbar) findViewById(R.id.toolbar1);
listView= (ListView) findViewById(R.id.listView1);
for (int i=1;i<10;i++){
arrayList.add(new FAdoptImage(R.mipmap.ic_launcher,”name”+i,”meseage”+i));
}
}
}
Adapter:
public class FAdoptAdapter extends BaseAdapter {
Context context;
ArrayList<FAdoptImage> pet;
LayoutInflater mLayoutInflater;
public FAdoptAdapter(Context context, ArrayList pet) {
this.context = context;
this.pet = pet;
mLayoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return pet.size();
}
@Override
public Object getItem(int i) {
return pet.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class ViewHolder{
ImageView imageView;
TextView textView1;
TextView textView2;
}
ViewHolder mViewHolder;
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// View v= LayoutInflater.from(context).inflate(R.layout.fadopt_list_item,null);
if(view==null){
view=mLayoutInflater.inflate(R.layout.fadopt_list_item,null);
mViewHolder = new ViewHolder();
mViewHolder.imageView= (ImageView) view.findViewById(R.id.imageView_item);
mViewHolder.textView1= (TextView) view.findViewById(R.id.textView_name);
mViewHolder.textView2= (TextView) view.findViewById(R.id.textView_juti);
view.setTag(mViewHolder);
}else {
mViewHolder= (ViewHolder) view.getTag();
}
mViewHolder.imageView.setImageResource((int) pet.get(i).getTouXiang());
mViewHolder.textView1.setText(pet.get(i).getName());
mViewHolder.textView2.setText(pet.get(i).getMeseage());
return view;
}
}
解决方案

20

    @Override
public int getCount() {
if(pet == null){
return 0;
}
return pet.size();
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明适配器总是出现空指针问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)