我无法更改文本字段的文本

时间:2014-06-25 01:31:05

标签: android fragment

我正在尝试更改FragmentPager适配器的文本字段,即DescripcionFragment。

我正在改变Local_vista

TextView desc =(TextView)findViewById(R.id.descripcion_desc);
        desc.setText(descripcion_local);

和texfield位于班级DescripcionFragment

并给我以下错误

Process: com.learn2crack.tuchillan, PID: 21845
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn2crack.tuchillan/com.learn2crack.tuchillan.FragmentLocal.Local_vista}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.learn2crack.tuchillan.FragmentLocal.Local_vista.onCreate(Local_vista.java:119)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Local_Vista

public void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FadingActionBarHelper helper = new FadingActionBarHelper()
                .actionBarBackground(R.drawable.ab_solid_tucc)
                .headerLayout(R.layout.header)
                .contentLayout(R.layout.vista_local);
        setContentView(helper.createView(this));
        helper.initActionBar(this);
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
        FragmentPagerAdapter.MyPagerAdapter adapterViewPager = new FragmentPagerAdapter.MyPagerAdapter(getSupportFragmentManager());

        vpPager.setAdapter(adapterViewPager);

        vpPager.setCurrentItem(1);

        // Attach the page change listener inside the activity
        vpPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            // This method will be invoked when a new page becomes selected.
            @Override
            public void onPageSelected(int position) {

            }

            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Code goes here
            }

            // Called when the scroll state changes:
            // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int state) {
                // Code goes here
            }


         });


TextView desc =(TextView)findViewById(R.id.descripcion_desc);
        desc.setText(descripcion_local);
}

http://i.imgur.com/xrI2nql.png

DescripcionFragment

public class DescripcionFragment extends Fragment  {

    // newInstance constructor for creating fragment with arguments
    public static DescripcionFragment newInstance(int page, String title) {
        DescripcionFragment fragmentsecond = new DescripcionFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentsecond.setArguments(args);
        return fragmentsecond;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("someInt", 0);
        title = getArguments().getString("someTitle");
    }

    // Inflate the view for the fragment based on layout XML
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.v_tab_desc, container, false);



        return view;
    }

1 个答案:

答案 0 :(得分:1)

您可能在onCreateView()中执行此操作,因此请使用使用inflater进行充气的视图,该视图具有充气视图的参考。

 TextView desc =(TextView)view.findViewById(R.id.descripcion_desc);
    desc.setText(descripcion_local);