将TextView设置为可见时崩溃

时间:2014-12-15 19:15:28

标签: java android textview

设置TextView在我的片段中可见时,我的应用程序崩溃了。

在onCreateView中,我得到了一个关于setVisibility()的NullPointerException

settingsTitle = (TextView)getActivity().findViewById(R.id.settingsTitle);
settingsTitle.setVisibility(View.VISIBLE);

这是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="@color/plain_background">

    <TextView
        android:id="@+id/settingsTitle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:text="Choose Your Default Screen"
        android:textAlignment="center"
        android:gravity="center"
        android:textColor="@color/black_text"
        android:visibility="gone"/>

    <ListView
        android:id="@+id/listFeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/settingsTitle"
        android:fadeScrollbars="true"
        android:fastScrollEnabled="true"
        android:fadingEdge="none"
        android:divider="@color/kfb_blue"
        android:dividerHeight="1dp"
        android:background="@color/plain_background"/>

</RelativeLayout>

以下是我的片段中所有代码的请求。

package kyfb.android.kyfb.com.kyfb;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class SettingsFragment extends Fragment {

    private ListView defaultScreen;
    private static SharedPreferences sharedPref;
    private static SharedPreferences.Editor editor;
    private static TextView settingsTitle;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_benefits, null);

        // this.getActivity().getActionBar().setTitle("Choose Default Screen");

        String[] values = new String[] {"Ag News", "Action Alerts", "Market Updates", "KFB Magazine", "Ag Facts",
                 "Member Benefits", "Roadside Farm Markets", "Media", "Annual Meeting", "My KYFB"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_single_choice, android.R.id.text1, values) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView text = (TextView) view.findViewById(android.R.id.text1);
                text.setTextColor(getResources().getColor(R.color.kfb_blue));
                return view;
            }
        };

        defaultScreen = (ListView)rootView.findViewById(R.id.listFeed);
        defaultScreen.setChoiceMode(defaultScreen.CHOICE_MODE_SINGLE);
        defaultScreen.setAdapter(adapter);

        settingsTitle = (TextView)getActivity().findViewById(R.id.settingsTitle);
        settingsTitle.setVisibility(View.VISIBLE);

        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        String savedDefault = sharedPref.getString("DEFAULT", null);
        if (savedDefault != null) {
            if (savedDefault.equals("ag news")) {
                defaultScreen.setItemChecked(0, true);
            }
            else if (savedDefault.equals("action alerts")) {
                defaultScreen.setItemChecked(1, true);
            }
            else if (savedDefault.equals("market updates")) {
                defaultScreen.setItemChecked(2, true);
            }
            else if (savedDefault.equals("kfb magazine")) {
                defaultScreen.setItemChecked(3, true);
            }
            else if (savedDefault.equals("ag facts")) {
                defaultScreen.setItemChecked(4, true);
            }
            else if (savedDefault.equals("member benefits")) {
                defaultScreen.setItemChecked(5, true);
            }
            else if (savedDefault.equals("roadside markets")) {
                defaultScreen.setItemChecked(6, true);
            }
            else if (savedDefault.equals("media")) {
                defaultScreen.setItemChecked(7, true);
            }
            else if (savedDefault.equals("annual meeting")) {
                defaultScreen.setItemChecked(8, true);
            }
            else if (savedDefault.equals("my kyfb")) {
                defaultScreen.setItemChecked(9, true);
            }
        }
        else {
            defaultScreen.setItemChecked(0, true);
        }

        defaultScreen.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(position) {
                    case 0:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "ag news");
                        editor.apply();

                        defaultScreen.setItemChecked(0, true);
                        break;
                    case 1:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "action alerts");
                        editor.apply();

                        defaultScreen.setItemChecked(1, true);
                        break;
                    case 2:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "market updates");
                        editor.apply();

                        defaultScreen.setItemChecked(2, true);
                        break;
                    case 3:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "kfb magazine");
                        editor.apply();

                        defaultScreen.setItemChecked(3, true);
                        break;
                    case 4:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "ag facts");
                        editor.apply();

                        defaultScreen.setItemChecked(4, true);
                        break;
                    case 5:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "member benefits");
                        editor.apply();

                        defaultScreen.setItemChecked(5, true);
                        break;
                    case 6:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "roadside markets");
                        editor.apply();

                        defaultScreen.setItemChecked(6, true);
                        break;
                    case 7:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "media");
                        editor.apply();

                        defaultScreen.setItemChecked(7, true);
                        break;
                    case 8:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "annual meeting");
                        editor.apply();

                        defaultScreen.setItemChecked(8, true);
                        break;
                    case 9:
                        sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                        editor = sharedPref.edit();
                        editor.putString("DEFAULT", "my kyfb");
                        editor.apply();

                        defaultScreen.setItemChecked(9, true);
                        break;
                    default:
                        break;
                }
            }
        });

        return rootView;
    }
}

2 个答案:

答案 0 :(得分:1)

问题是因为我使用的是getActivity.findViewById()而不是rootView.findViewById()

答案 1 :(得分:0)

您从哪里调用此代码?我假设您正在使用片段,因为“getActivity()”部分

这里发生了两件事中的一件:

  1. getAtivity()为null,如果您的片段尚未附加到活动,则可能会发生这种情况。你可以通过在if语句中包含代码来解决这个问题:

    if(isAdded()){
        settingsTitle = (TextView)getActivity().findViewById(R.id.settingsTitle);
        settingsTitle.setVisibility(View.VISIBLE);
    }
    
  2. 您的布局尚未膨胀,因此屏幕上没有“settingsTitle”ID的视图。如果从onCreateActivity调用此代码,则应引用新添加的视图而不是getActivity()。findView

  3. 需要更多代码才能为您提供具体的解决方案。