简体   繁体   English

选择导航抽屉项时更改TextView

[英]Change TextView when Navigation Drawer Item is selected

I'm trying to make three different options in my navigation drawer use the same fragment but with different values in the TextView in that fragment. 我试图使导航抽屉中的三个不同选项使用相同的片段,但在该片段的TextView中使用不同的值。

I created a standard Navigation Drawer Activity with Android Studio and tried to use setText in the same method that changes the title, see below: 我使用Android Studio创建了一个标准的Navigation Drawer Activity,并尝试在更改标题的相同方法中使用setText,如下所示:

public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                Namn.setText("Item 1");
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                Namn.setText("Item 2");
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                Namn.setText("Item 3");
                break;
        }
    }

When I try to run the app it force closes instantly and I get this error: 当我尝试运行应用程序时,它会立即关闭,并且出现以下错误:

Process: com.test.je.test, PID: 22605
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.je.test/com.test.je.test.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            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:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.test.je.test.MainActivity.onSectionAttached(MainActivity.java:68)
            at com.test.je.test.MainActivity$PlaceholderFragment.onAttach(MainActivity.java:153)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
            at android.app.Activity.performStart(Activity.java:5251)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2160)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            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:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)

I have declared the TextView with TextView Namn; 我已经用TextView Namn声明了TextView; and Namn = (TextView)findViewById(R.id.twnamn); 和Namn =(TextView)findViewById(R.id.twnamn);

What am I doing wrong here? 我在这里做错了什么?

So user @feresr helped me understand what was wrong with his comment. 因此,用户@feresr帮助我了解了他的评论出了什么问题。

I created an if statement in the onCreateView() method instead and it solved the problem. 我在onCreateView()方法中创建了一个if语句,它解决了问题。

I had to declare the textview after the fragment had been created also. 在创建片段之后,我还必须声明textview。

Here is the code: 这是代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;
            Bundle args = getArguments();
            int currentView = getArguments().getInt(ARG_SECTION_NUMBER);

            if(currentView == 1){
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
                Namn.setText("Val 1");
            }else if(currentView == 2){
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
                Namn.setText("Val 2");
            }else if(currentView == 3){
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
                Namn.setText("Val 3");
            }else {
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
            }


            return rootView;
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM