customAdapter 中的运行时错误

runtime error in customAdapter

我正在尝试使用自定义适配器创建卡片视图列表。我已经在单独的 .xml 文件中定义了单行列表的布局,其中包含卡片视图和其中的 imageview/textviews。我正在使用自定义 srrsy 适配器。当我尝试打开具有列表视图的 activity 时,我的应用程序崩溃,仅给出运行时异常错误。 错误: AndroidRuntime(2583): 在 graph.prathya.com.nextstepz.CustomAdapters.PostArrayAdapter.getView(PostArrayAdapter.java:44)

这个错误在行

LayoutInflater li =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

在自定义适配器中。

这里是single_card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll1">


<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/ll2">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:id="@+id/imgIcon"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/ll3"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SampleTitle1"
            android:layout_marginTop="10dp"
            android:background="#00b5ad"
            android:textSize="20dp"
            android:gravity="center"
            android:padding="5dp"
            android:id="@+id/title"
            android:textColor="#ffffff"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sampledisription1"
            android:layout_marginTop="10dp"
            android:background="#ffffff"
            android:textSize="10dp"
            android:gravity="center"
            android:padding="5dp"
            android:id="@+id/desciption"
            />

    </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

这里是PostArrayAdapter.java

package graph.prathya.com.nextstepz.CustomAdapters;

 import android.app.Activity;
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup; 
 import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;
 import graph.prathya.com.nextstepz.R;


 /**
  * Created by Prathya on 5/23/2015.
  */
public class PostArrayAdapter extends ArrayAdapter<Post>{
Context context;
Post data[] =null;
int layoutid;

public PostArrayAdapter(Context context, int layoutid, Post data[]) {
    super(context,layoutid);
    this.data=data;
    this.layoutid=layoutid;
}

private class PostHolder{
    ImageView imgIcon;
    TextView title,description;
}

@Override
public int getCount(){
    return data.length;
}

@Override
public View getView(int Position, View convertView, ViewGroup parent){
    PostHolder holder;
    View v = convertView;
    if(v==null){
        LayoutInflater li = LayoutInflater.from(context);/* RunTimeExceptio error at this line of code */
        v= li.inflate(layoutid,parent,false);
        holder = new PostHolder();
        holder.imgIcon = (ImageView)v.findViewById(R.id.imgIcon);
        holder.title = (TextView)v.findViewById(R.id.title);
        holder.description= (TextView)v.findViewById(R.id.desciption);
        v.setTag(holder);
    }
    else {
        holder = (PostHolder)v.getTag();
     }

      Post post = data[Position];
       holder.imgIcon.setImageResource(post.imgIcon);
        holder.title.setText(post.title);
      holder.description.setText(post.description);
     return  v;
    }

}

这里是 activity,其中 listView 位于:HomeScreenAvtivity.java

package graph.prathya.com.nextstepz;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import graph.prathya.com.nextstepz.Communicator.Communicater1;
import graph.prathya.com.nextstepz.CustomAdapters.Post;
import graph.prathya.com.nextstepz.CustomAdapters.PostArrayAdapter;

public class HomeScreenActivity extends ActionBarActivity {
 ImageButton passionbtn, eventbtn, projectbtn, groupstudybtn;
 Dialog dg;
ListView listView = null;
Post post[] =new Post[] {
        new Post(R.drawable.img1,"Cats and Children","Cats can be a      fascinating experience for children, but young minds can sometimes confuse a pet for a toy. Teach children how to respect and properly handle your cat for best results."),
        new Post(R.drawable.img2,"Android:The Best OS","Android powers hundreds of millions of mobile devices in more than 190 countries around the world.Android’s openness has made it a favorite for consumers."),
        new Post(R.drawable.img3,"Beautiful","Monica is an Italian actor and model who started her modelling career at the age of 13 by posing for a local photo enthusiast."),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    listView = (ListView)findViewById(R.id.homelist);
   listView.setAdapter(new PostArrayAdapter(getApplicationContext(),R.layout.single_card_view,post));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub


    if (item.getItemId() == R.id.nxtsignupitem) {
        Intent in = new Intent(getApplicationContext(), PostActivity.class);
        startActivity(in);
    }

    if (item.getItemId() == R.id.eventitem1) {
        Intent in = new Intent(getApplicationContext(), PostDetailActivity.class);
        startActivity(in);
    }
    if (item.getItemId() == R.id.postitem11) {

        dg = new Dialog(HomeScreenActivity.this);
        dg.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dg.setContentView(R.layout.fragment_new_post_dialogue);
        dg.setTitle("Please choose One option");
        dg.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dg.show();

        passionbtn = (ImageButton) dg.findViewById(R.id.imageButton1st);
        eventbtn = (ImageButton) dg.findViewById(R.id.imageButton2);
        projectbtn = (ImageButton) dg.findViewById(R.id.imageButton3);
        groupstudybtn = (ImageButton) dg.findViewById(R.id.imageButton4);

        passionbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Communicater1.setpostButtonid(1);
                Intent in = new Intent(getApplicationContext(), PostActivity.class);
                startActivity(in);
            }

        });
        eventbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Communicater1.setpostButtonid(2);
                Intent in = new Intent(getApplicationContext(), PostActivity.class);
                startActivity(in);
            }

        });
        projectbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Communicater1.setpostButtonid(3);
                Intent in = new Intent(getApplicationContext(), PostActivity.class);
                startActivity(in);
            }

        });
        groupstudybtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Communicater1.setpostButtonid(4);
                Intent in = new Intent(getApplicationContext(), PostActivity.class);
                startActivity(in);
            }

        });


    }
    return super.onOptionsItemSelected(item);}
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // TODO Auto-generated method stub

        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.menu_home_screen, menu);
        return super.onCreateOptionsMenu(menu);

    }      }

XML HomeScreenActivity 的布局文件

   <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/DrawerLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillViewport="true">
  <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 >
    <ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/lord"
    android:layout_marginBottom="20dp"
     />
   <ListView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/homelist">
   </ListView>
  </LinearLayout>
  </ScrollView>

Post.java 用于适配器

package graph.prathya.com.nextstepz.CustomAdapters;

/**
 * Created by Prathya on 5/23/2015.
*/
public class Post {
int imgIcon;
String title,description;

 public Post(int imgIcon,String title, String description){
    this.imgIcon=imgIcon;
    this.title=title;
    this.description=description;
 }
 }

您永远不会在构造函数中分配 context

添加

this.context = context;

给你的构造函数