尝试使用settext函数时出现空指针异常

时间:2014-06-27 09:08:43

标签: android android-layout android-activity android-fragments

我有2个导航标签,每个导航标签都包含一个列表视图。每个列表视图由几个组件组成,包括我正在尝试编辑的文本视图。

我试图在定义每个片段的类中为文本视图设置text,但是我得到一个空指针异常。

以下是一些代码摘录:

查看寻呼机

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

每个片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

</LinearLayout>

每个列表视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/label"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toLeftOf="@+id/thumbnail"
    android:text="address"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

....

这是我的适配器的代码:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public ListFragment getItem(int index) {
        switch (index) {
            case 0:
                return new ArabicFragment();
            case 1:
                return new EnglishFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }
}

在这个类中我定义了标签:

public class TaskListAndOptionsActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private static ActionBar actionBar;
    private static Context context; 

    // Tab titles
    private String[] tabs = { "Arabic", "English"};

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_task_list_and_options);

        viewPager = (ViewPager) findViewById(R.id.titleTaskList);

        actionBar = getActionBar();

        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
        getActionBar().setCustomView(R.layout.actionbar);
        getActionBar().setDisplayShowHomeEnabled(true);

        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);

        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

             @Override
             public void onPageScrolled(int arg0, float arg1, int arg2) {
             }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
         });
}

...

最后在这个类中我正在尝试settext(这个类适用于每个片段)

public class ArabicFragment extends ListFragment {
public static ArrayList<String> taskListAr = new ArrayList<String>();
private static Context context;
private static LayoutInflater inflater=null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("changing layout","arabic");
    View rootView = inflater.inflate(R.layout.fragment_tasklistar, container, false);  
    return rootView;
}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayTaskList();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_itemar,R.id.title, taskListAr);
        setListAdapter(adapter);
}


    TextView taskDetails = (TextView)getView().findViewById(R.id.taskdetails);
    taskDetails.setText("trial");

...

如果这不是调用settext的正确位置,那么它应该在哪里调用?

2 个答案:

答案 0 :(得分:1)

您必须使用 onCreateView 方法编写。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("changing layout","arabic");
        View rootView = inflater.inflate(R.layout.fragment_tasklistar, container, false);
        TextView taskDetails = (TextView)rootView.findViewById(R.id.taskdetails);
        taskDetails.setText("trial");
        return rootView;
    }

答案 1 :(得分:0)

创建一个方法并使用View作为参数并使用它来执行任何类型的初始化..如下所示:

private void initialze(View mView){
TextView taskDetails = (TextView)mView.findViewById(R.id.taskdetails);
taskDetails.setText("trial");
} 

并使用onCreateView()中的方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {
Log.d("changing layout","arabic");
View rootView = inflater.inflate(R.layout.fragment_tasklistar, container, false);  
initialze(rootView );
return rootView;
}

它很容易,也遵循android编程标准。