从另一个Android类修改自定义视图

时间:2015-09-05 12:00:57

标签: java android xml

我在MyView类中创建了一个自定义视图,并希望在我的MainActivity类中使用mLevel方法显示或隐藏onSharedPreferenceChanged

以下是MyView.java

public class MyView extends LinearLayout {

    private final TextView mLevel;

    public StatusBarView(Context context) {
        this(context, null, 0);
    }

    public StatusBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatusBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setGravity(Gravity.CENTER_VERTICAL);

        LayoutInflater.from(context).inflate(R.layout.my_level_view, this);

        mLevel = (TextView) findViewById(R.id.levelTextView);
    }

    public void showLevel(boolean bShowLevel) {
    if (bShowLevel) {
        mLevel.setVisibility(View.VISIBLE);
        mLevel.setText(mService.getLevel());
    } else {
        mLevel.setVisibility(View.GONE);
    }
}
}

我在这里MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
}

public class PrefsFragment extends PreferenceFragment
        implements SharedPreferences.OnSharedPreferenceChangeListener {

    private TextView mLevel;

    private LevelService mLevelService = new LevelService();

    private MyView mMyView = new MyView(getBaseContext());

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        mLevel = (TextView) findViewById(R.id.levelTextView);
    }

    @Override
    public void onResume() {
        super.onResume();

        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

        if (key.equals("showLevel")) {
             mMyView.showLevel(prefs.getBoolean("showLevel", false));       
        }

    }

}

但是,当我使用showLevel键点击复选框时,我的应用会崩溃。在Android Studio中,我从这一行开始得到NullPointerException:mLevel.setVisibility(View.VISIBLE);当试图从另一个类而不是MyView(它工作得很好)修改View时。{/ p>

非常感谢任何帮助。

更新:我在showLevel()内添加MyView方法并尝试在服务中访问它。工作

1 个答案:

答案 0 :(得分:0)

用,

代替您的代码
public class MainActivity extends AppCompatActivity implements SetVisibilityInterface{

private TextView mLevel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLevel = (TextView) findViewById(R.id.levelTextView);
    // Display the fragment as the main content.
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment(this)).commit();
}

@Override
public void setVisibilityInterfaceAndData(int visibility, String value) {
    if(visibility==View.VISIBLE){
        mLevel.setVisibility(View.VISIBLE);
        mLevel.setText(value);
    }else {
        mLevel.setVisibility(View.GONE);
    }
}

public class PrefsFragment extends PreferenceFragment
        implements SharedPreferences.OnSharedPreferenceChangeListener {

    private TextView mLevel;
    private Context mContext;
    private LevelService mLevelService = new LevelService();
    private SetVisibilityInterface mySetVisibilityInterface;

    public PrefsFragment(Context context) {
        mContext = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals("showLevel")) {
            try {
                mySetVisibilityInterface = (SetVisibilityInterface) mContext;
                if (prefs.getBoolean("showLevel", false)) {
                    mySetVisibilityInterface.setVisibilityInterfaceAndData(View.VISIBLE, mService.getLevel());
                } else {
                    mySetVisibilityInterface.setVisibilityInterfaceAndData(View.GONE, "");
                }
            } catch (ClassCastException e) {
                throw new ClassCastException(mContext.toString()
                        + " must implement SetVisibilityInterface");
            }
        }
    }
}
}

对于callBack,你需要接口,

public interface SetVisibilityInterface {
public void setVisibilityInterfaceAndData(int visibility, String value);
}

让我知道它是否适合你,

并标记为答案,以便对其他人有用......