在Fragment崩溃中插入自定义列表视图

时间:2014-11-27 22:51:38

标签: android listview fragment

我是Android编程的新手。

所以我按照一些关于自定义Listviews和Fragments的教程进行了很好的工作,但是现在我很难完成这两项工作。

我有12个片段,每个片段代表一个月。

我正试图让Fragview中的Listview工作。

也许我这样做完全错了?我想不出来...... :(我现在已经坚持了几天。

每当我尝试启动应用程序时,它都会崩溃。

11-27 23:42:38.770    5175-5175/de.swipeview5.stefan.swipeview5 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d4d300)
11-27 23:42:38.775    5175-5175/de.swipeview5.stefan.swipeview5 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at de.swipeview5.stefan.swipeview5.FragmentJanuar.onCreateView(FragmentJanuar.java:43)

所以我得到了名为“FragmentJanuar.java”的片段和名为“fragment_januar.xml”的xml文件


fragment_januar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">

    <TextView
        android:layout_margin="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:text="Januar 2015"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listview_januar"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>


FragmentJanuar.java

package de.swipeview3.stefan.swipeview3;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Stefan on 25.11.2014.
 */

public class FragmentJanuar extends Fragment
        //implements ListView.OnItemClickListener
        {

    private ArrayList<meetings> myMeetingsJanuar = new ArrayList<meetings>();
    private ContactsAdapter adapter;
    private ListView lv;

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

        View v = inflater.inflate(R.layout.item_view, container, false);
        meetings one = new meetings("Event 1", "01.01.2001", "01:00", "1€", "Street 1", 0, "www.event1url.de");
        meetings two = new meetings("Event 1", "01.01.2001", "01:00", "1€", "Street 1", 0, "www.event1url.de");

        myMeetingsJanuar.add(one);
        myMeetingsJanuar.add(two);

                lv =(ListView)v.findViewById(R.id.listview_januar);

                adapter = new ContactsAdapter(getActivity(), myMeetingsJanuar);

                lv.setAdapter(adapter);

//        return v;
        return inflater.inflate(R.layout.fragment_januar, container, false);

    }

    private class ContactsAdapter extends BaseAdapter {

        private FragmentActivity activity;
        private LayoutInflater inflater;
        private ArrayList<meetings> data;

        public ContactsAdapter (FragmentActivity a, ArrayList<meetings> d){
            activity = a;
            data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

        @Override
        public meetings getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            v = inflater.inflate(R.layout.item_view, null);


            meetings currentMeeting = myMeetingsJanuar.get(position);

            //Eventname
            TextView Event = (TextView) v.findViewById(R.id.item_txtEvent);
            Event.setText(currentMeeting.getEventname());

            //date
            TextView Date = (TextView) v.findViewById(R.id.item_txtDate);
            Date.setText(currentMeeting.getDate());

            //Time
            TextView Time = (TextView) v.findViewById(R.id.item_txtTime);
            Time.setText("Uhrzeit: " + currentMeeting.getTime() + "Uhr" );

            //Price
            TextView Price = (TextView) v.findViewById(R.id.item_txtPrice);
            Price.setText("Eintritt: "+ currentMeeting.getPrice());

            //Location
            TextView Location = (TextView) v.findViewById(R.id.item_txtLocation);
            Location.setText("Location: " + currentMeeting.getLocation());

            //EventUrl
            TextView Eventurl = (TextView) v.findViewById(R.id.item_txtEventurl);
            Eventurl.setText("Url: " + currentMeeting.getEventurl());

            return v;
        }
    }

}


会议班:

public class meetings {
    private String eventname;
    private String date;
    private String time;
    private String price;
    private String location;
    private int iconID;
    private String eventurl;


    public meetings(String eventname, String date, String time, String price, String location, int iconID, String eventurl){
        super();
        this.eventname = eventname;
        this.date = date;
        this.time = time;
        this.price = price;
        this.location = location;
        this.iconID = iconID;
        this.eventurl = eventurl;
    }

    public String getEventname(){
        return eventname;
    }
    public String getDate(){
        return date;
    }
    public String getTime(){
        return time;
    }
    public String getPrice(){
        return price;
    }
    public String getLocation(){
        return location;
    }
    public String getEventurl(){
        return eventurl;
    }
}


MainActivity.java

package de.swipeview5.stefan.swipeview5;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    ViewPager viewpager=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager= (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager=getSupportFragmentManager();
        viewpager.setAdapter(new MyAdapter(fragmentManager));
    }
}

class MyAdapter extends FragmentStatePagerAdapter
{
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment=null;
        if(i==0)
        {
            fragment=new FragmentJanuar();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return 1;
    }

  
  
    @Override
    public CharSequence getPageTitle(int position) {
        if(position==0)
        {
            return "Januar 2015";
        }

        return null;
    }
}


activity_main.xml中

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/pager">

    <android.support.v4.view.PagerTitleStrip
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/title"
        android:background="#33B5E5"
        android:layout_gravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp">

    </android.support.v4.view.PagerTitleStrip>

</android.support.v4.view.ViewPager>


listview

中自定义行布局的 item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_Eventicon"
        android:maxHeight="80dp"
        android:maxWidth="80dp"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:src="@drawable/abc_btn_check_to_on_mtrl_000"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/item_txtEvent"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="01.01.2000"
        android:id="@+id/item_txtDate"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="15dp"
        android:textColor="#ffffff"
        android:singleLine="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Eventname"
        android:id="@+id/item_txtEvent"
        android:layout_toEndOf="@+id/item_txtDate"
        android:layout_below="@+id/item_txtDate"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="14dp"
        android:textSize="30dp"
        android:textColor="#ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Uhrzeit"
        android:id="@+id/item_txtTime"
        android:textSize="10dp"
        android:layout_below="@+id/item_txtLocation"
        android:layout_alignLeft="@+id/item_txtLocation"
        android:layout_alignStart="@+id/item_txtLocation"
        android:textColor="#ffb0baff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Preis"
        android:id="@+id/item_txtPrice"
        android:textSize="10dp"
        android:layout_toEndOf="@+id/item_txtDate"
        android:layout_above="@+id/item_txtEventurl"
        android:layout_toRightOf="@+id/item_txtTime"
        android:layout_marginLeft="40dp"
        android:textColor="#ffb0baff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Location"
        android:id="@+id/item_txtLocation"
        android:textSize="15dp"
        android:layout_below="@+id/item_txtEvent"
        android:layout_alignLeft="@+id/item_txtEvent"
        android:layout_alignStart="@+id/item_txtEvent"
        android:textColor="#ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Eventurl"
        android:id="@+id/item_txtEventurl"
        android:layout_toEndOf="@+id/item_txtTime"
        android:textSize="15dp"
        android:layout_toStartOf="@+id/item_Eventicon"
        android:layout_below="@+id/item_txtTime"
        android:layout_alignLeft="@+id/item_txtTime"
        android:layout_alignStart="@+id/item_txtTime"
        android:textColor="#ffb0baff" />
</RelativeLayout>

我让Listview独自在另一个应用程序中运行得很好,但现在我正试图让它在Fragment中运行。我可以毫无错误地运行代码,但Listview不会显示在Fragment中。我希望这不会搞砸。

如果您还有其他需要,请告诉我。

谢谢。

1 个答案:

答案 0 :(得分:-1)

看起来你在onCreateView()的{​​{1}}中夸大了错误的布局; (FragmentJanuar课程中应item_view.xml加注。您实际使用上述代码执行的操作是将列表项布局设置为片段的布局。试试这个:

ContactsAdapter