我正在尝试设置EditText的文本,但出现了空指针异常。我已经看到其他帖子回答了这个问题,但是我正在扩展ActionBarActivity,所以我不能简单地通过使用诸如EditText f1 =(EditText)getActivity(). findViewById(R.id.field1)之类的东西来解决此问题。
public class SwitchURL extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch_url);
Intent intent = getIntent();
getActionBar().setDisplayHomeAsUpEnabled(true);
EditText f1 = (EditText) findViewById(R.id.field1);
EditText f2 = (EditText)findViewById(R.id.field2);
SharedPreferences pref1 = getSharedPreferences("textField1", 0);
SharedPreferences pref2 = getSharedPreferences("textField1", 0);
Log.v("", pref1.getString("textField1", "DNE"));
Log.v("", pref2.getString("textField2", "DNE"));
f1.setText("hello", TextView.BufferType.EDITABLE);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
我知道我正在获取NullPointerException,因为我没有访问包含此editText的正确视图。我有一个单击按钮时会调用的方法,它可以正常工作。
public void changeUrl(View v) {
EditText f1 = (EditText)findViewById(R.id.field1);
EditText f2 = (EditText)findViewById(R.id.field2);
SharedPreferences pref1 = getSharedPreferences("textField1", 0);
SharedPreferences pref2 = getSharedPreferences("textField1", 0);
SharedPreferences.Editor edit1 = pref1.edit();
SharedPreferences.Editor edit2 = pref2.edit();
edit1.putString("textField1", f1.getText().toString());
edit2.putString("textField2", f2.getText().toString());
edit1.commit();
edit2.commit();
Log.v("", pref1.getString("textField1", "Fail"));
Log.v("", pref2.getString("textField2", "Fail"));
}
知道如何访问此视图以便设置editText的文本吗?这是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_native.SwitchURL$PlaceholderFragment"
android:background="@color/white"
android:orientation="vertical"
android:id="@+id/contain" >
<TextView
android:id="@+id/urlScreenTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change URL"
android:textColor="@color/black"
android:textSize="30sp"
android:layout_gravity="center"/>
<EditText
android:id="@+id/field1"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="http://stage1.onshift.com"
android:textColor="@color/black"
android:textSize="17sp"
android:background="@drawable/switch_url_fields_background"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="18dp"
android:paddingLeft="7dp"/>
<EditText
android:id="@+id/field2"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="http://stage.v2.onshift.com"
android:textColor="@color/black"
android:textSize="17sp"
android:background="@drawable/switch_url_fields_background"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:paddingLeft="7dp"/>
<Button
android:id="@+id/switchUrlButton"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="Change URL"
android:textColor="@color/white"
android:textSize="22sp"
android:background="@drawable/switch_url_button"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="13dp"
android:gravity="center"
android:onClick="changeUrl"/>
</LinearLayout>
看起来您正在尝试从活动中访问片段布局中定义的视图。活动在创建时不知道片段布局。将获取视图并管理其内容的代码移动到片段的onCreatView()中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句