我的应用程序使用主要活动和片段。我在片段下设置了swipelistview
,其中包含自定义列表视图。执行时,应用程序崩溃,日志cat报告以下错误:
02-17 15:20:17.897:E / AndroidRuntime(5759):引起:
java.lang.NullPointerException 02-17 15:20:17.897:
E / AndroidRuntime(5759):在 com.fragments.Frag1.onCreateView(Frag1.java:71)
以下陈述位于Frag1.java
的第71行:
swipelistview =(SwipeListView)getView()findViewById(R.id.example_swipe_lv_list);
我在调用example_swipe_lv_list
时夸大了XML中的getView()
项。
我正在http://www.tutecentral.com/android-swipe-listview/测试示例,但是有片段。
可能导致此问题的原因是什么?
这是完整的片段代码:
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.ItemAdapter;
import com.example.ItemRow;
import com.example.R;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView;
public class MyList extends Fragment {
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
index = list.getFirstVisiblePosition();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
list.setSelectionFromTop(index, 0);
}
ListView list;
int index = 0;
MediaPlayer mp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
{
mp=MediaPlayer.create(getActivity(), R.raw.1);
return inflater.inflate(R.layout.listview, container, false);
}
@Override
public void onStart() {
super.onStart();
list=(ListView)getView().findViewById(R.id.listView1);
list.setAdapter(new MyAdapter(getActivity()));
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
switch(position)
{
case 0:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.1);
mp.start();
break;
case 1:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.2);
mp.start();
break;
case 2:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.3);
mp.start();
break;
case 3:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.4);
mp.start();
break;
case 4:
mp.release();
mp=MediaPlayer.create(getActivity(), R.raw.5);
mp.start();
break;
}
}
});
}
}
class MyAdapter extends BaseAdapter
{
ArrayList<SingleRow> list;
Context context;
public MyAdapter(Context c) {
context=c;
list=new ArrayList<SingleRow>();
//Initialize them...
Resources res=c.getResources();
String[] dialogues = res.getStringArray(R.array.dialogues);
int[] images={
R.drawable.ics,
R.drawable.ics,
R.drawable.ics,
R.drawable.ics,
R.drawable.ics,
R.drawable.ics,
R.drawable.ics,
};
for(int i=0;i<38;i++)
{
list.add(new SingleRow(dialogues[i],images[i]));
}
}
public int convertDpToPixel(float dp) {
DisplayMetrics metrics =context.getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
MyViewHolder holder=null;
itemData=new ArrayList<ItemRow>();
adapter=new ItemAdapter(context,R.layout.custom_row,itemData);
SwipeListView swipelistview;
swipelistview.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
}
@Override
public void onClosed(int position, boolean fromRight) {
}
@Override
public void onListChanged() {
}
@Override
public void onMove(int position, float x) {
}
@Override
public void onStartOpen(int position, int action, boolean right) {
Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
}
@Override
public void onStartClose(int position, boolean right) {
Log.d("swipe", String.format("onStartClose %d", position));
}
@Override
public void onClickFrontView(int position) {
Log.d("swipe", String.format("onClickFrontView %d", position));
swipelistview.openAnimate(position); //when you touch front view it will open
}
public void onClickBackView(int position) {
Log.d("swipe", String.format("onClickBackView %d", position));
swipelistview.closeAnimate(position);//when you touch back view it will close
}
public void onDismiss(int[] reverseSortedPositions) {
}
});
//These are the swipe listview settings. you can change these
//setting as your requirement
swipelistview.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT); // there are five swiping modes
swipelistview.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL); //there are four swipe actions
swipelistview.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
swipelistview.setOffsetLeft(convertDpToPixel(260f)); // left side offset
swipelistview.setOffsetRight(convertDpToPixel(0f)); // right side offset
swipelistview.setAnimationTime(50); // Animation time
swipelistview.setSwipeOpenOnLongPress(true); // enable or disable SwipeOpenOnLongPress
swipelistview.setAdapter(adapter);
for(int i=0;i<10;i++)
{
itemData.add(new ItemRow("item"+i,getResources().getDrawable(R.drawable.ic_launcher) ));
}
adapter.notifyDataSetChanged();
if(row==null)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.list_rows, parent, false);
swipelistview = (SwipeListView)row.findViewById(R.id.example_swipe_lv_list);
row.setBackgroundResource(R.drawable.coloria);
holder=new MyViewHolder(row);
SingleRow temp=list.get(position);
holder.imageView.setImageResource(temp.image);
row.setTag(holder);
}
else
{
holder=(MyViewHolder)row.getTag();
}
SingleRow temp=list.get(position);
holder.textView.setText(temp.description);
Typeface font = Typeface.createFromAsset(holder.textView.getContext().getAssets(), "fonts/OpenSans.ttf");
holder.textView.setTypeface(font);
return row;
}
class MyViewHolder
{
ImageView imageView;
TextView textView;
MyViewHolder(View v)
{
textView=(TextView)v.findViewById(R.id.textView1);
imageView=(ImageView)v.findViewById(R.id.imageView1);
}
}
class SingleRow
{
String description;
int image;
SingleRow(String description, int image)
{
this.description=description;
this.image=image;
}
}
}
答案 0 :(得分:3)
在调用或完成getView()
之前,您无法使用onCreateView()
(因此您也无法在onCreateView()
内使用它。)
您可以使用膨胀的视图来调用findViewById()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.some_fragment, container, false);
swipelistview=(SwipeListView)view.findViewById(R.id.example_swipe_lv_list);
return view;
}