1

我正在尝试在片段中实现一个列表视图,我使用 BaseAdapter 但我在日志猫上收到错误,这是错误:

10-31 15:41:10.921: E/AndroidRuntime(1246): FATAL EXCEPTION: main
10-31 15:41:10.921: E/AndroidRuntime(1246): java.lang.NullPointerException
10-31 15:41:10.921: E/AndroidRuntime(1246): at     info.androidhive.tabsswipe.NewsActivity.onCreateView(NewsActivity.java:28)

...我已经做到了

package info.androidhive.tabsswipe;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;

public class NewsActivity extends Fragment{

public ListView liste;
//public GridView grid;
public static Fragment newInstance(Context c){
    NewsActivity NA=new NewsActivity();
    return NA;  
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = (View)inflater.inflate(R.layout.fragment_news,container,false);
    liste=(ListView)rootView.findViewById(R.id.liste);
    liste.setAdapter(new MonAdapter(getActivity()));
    return rootView;
}

}

和适配器

package info.androidhive.tabsswipe;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MonAdapter extends BaseAdapter {
NewsActivity na=new NewsActivity();
private Context ctx;
private Integer[] vars={
        R.drawable.ic_launcher
};

public MonAdapter(Context c){
    ctx=c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return vars.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    TextView nom=new TextView(ctx);
    TextView prenom=new TextView(ctx);
    TextView num=new TextView(ctx);
    LinearLayout lt=new LinearLayout(ctx);
    lt.setOrientation(LinearLayout.VERTICAL);

    LinearLayout l2=new LinearLayout(ctx);
    l2.setOrientation(LinearLayout.HORIZONTAL); 

    nom.setText("AAAA");
    prenom.setText("AAAAA");
    num.setText("AAAAA");

    l2.addView(nom);
    l2.addView(prenom);

    lt.addView(l2);
    lt.addView(num);
    return lt;
}

}

作为 getcount 方法的回报,我添加了表变量的长度,因为我在列表视图中有一个元素。

4

0 回答 0