我有 5 个编辑文本,它们在SharedPreferences
. 但似乎NullPointerException
每次我呼吁这些价值观时我都会得到一个:
布局:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/smsSettingsBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="@dimen/ssBackPadding"
android:src="@drawable/ic_upblue" />
<ImageView
android:id="@+id/smsIconSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_smsblue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/smsIconSettings"
android:layout_centerHorizontal="true"
android:text="Quick SMS"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/iconOptionsSize" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/ssllMarginSides"
android:layout_marginRight="@dimen/ssllMarginSides"
android:layout_marginTop="@dimen/ssllMarginTop"
android:orientation="vertical"
android:padding="@dimen/smallPadding" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Quick SMS settings"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/ssqsText" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssqsDesMarginTop"
android:text="@string/smsSettingsBody"
android:textColor="@color/settingsBodyFont"
android:textSize="@dimen/ssqsDesTextSize" />
<EditText
android:id="@+id/phoneNumber1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssETMarginTop"
android:hint="Enter Mobile Number"
android:inputType="number" />
<TextView
android:id="@+id/addMe1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssAddNoMarginTop"
android:text="@string/addNumber"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/ssAddNoTextSize" />
<LinearLayout
android:id="@+id/removeable1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="fill_parent"
android:phoneNumber="@+id/phoneNumber2"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/sshlETMarginTop"
android:hint="Enter Mobile Number"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="7dp"
android:paddingRight="10dp"
android:text="Remove"
android:id="@+id/removeMe1"
android:textColor="@color/redText"
android:textSize="@dimen/ssAddNoTextSize" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssAddNoMarginTop"
android:text="@string/addNumber"
android:id="@+id/addMe2"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/ssAddNoTextSize" />
</LinearLayout>
<LinearLayout
android:id="@+id/removeable2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="fill_parent"
android:phoneNumber="@+id/phoneNumber3"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/sshlETMarginTop"
android:hint="Enter Mobile Number"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="7dp"
android:paddingRight="10dp"
android:text="Remove"
android:id="@+id/removeMe2"
android:textColor="@color/redText"
android:textSize="@dimen/ssAddNoTextSize" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssAddNoMarginTop"
android:text="@string/addNumber"
android:id="@+id/addMe3"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/ssAddNoTextSize" />
</LinearLayout>
<LinearLayout
android:id="@+id/removeable3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="fill_parent"
android:phoneNumber="@+id/phoneNumber4"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/sshlETMarginTop"
android:hint="Enter Mobile Number"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="7dp"
android:paddingRight="10dp"
android:text="Remove"
android:id="@+id/removeMe3"
android:textColor="@color/redText"
android:textSize="@dimen/ssAddNoTextSize" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ssAddNoMarginTop"
android:text="@string/addNumber"
android:id="@+id/addMe4"
android:textColor="@color/settingsTitleFont"
android:textSize="@dimen/ssAddNoTextSize" />
</LinearLayout>
<LinearLayout
android:id="@+id/removeable4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="fill_parent"
android:phoneNumber="@+id/phoneNumber5"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/sshlETMarginTop"
android:hint="Enter Mobile Number"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="7dp"
android:paddingRight="10dp"
android:text="Remove"
android:id="@+id/removeMe4"
android:textColor="@color/redText"
android:textSize="@dimen/ssAddNoTextSize" />
</RelativeLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/helpDespTVMarginTop"
android:text="@string/helpMessage"
android:textColor="@color/settingsBodyFont"
android:textSize="@dimen/helpDespTVTextSize" />
<EditText
android:id="@+id/helpMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/helpMessageTextSize"
android:background="@drawable/textview_shape"
android:padding="@dimen/helpMessagePadding"
android:text="@string/helpMessageBody"
android:textColor="#333333"
android:textSize="@dimen/helpMessageTextSize" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/helpDespTVMarginTop"
android:text="Send Quick SMS after:"
android:textColor="@color/settingsBodyFont"
android:textSize="@dimen/helpDespTVTextSize" />
<RelativeLayout
android:layout_width="150dp"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/delayTimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="30dp"
android:text="5"
android:textColor="#333333"
android:textSize="30sp" />
<TextView
android:id="@+id/secTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:text="seconds"
android:textColor="@color/settingsBodyFont"
android:textSize="14sp" />
</RelativeLayout>
<Button
android:id="@+id/saveNumbers"
android:layout_width="fill_parent"
android:layout_height="@dimen/ssSaveButtonHeight"
android:layout_marginTop="@dimen/ssSaveButtonMarginTop"
android:background="@drawable/button_save"
android:text="Save"
android:textColor="#FFFFFF"
android:textSize="@dimen/actionButton" />
</LinearLayout>
</ScrollView>
初始声明:
TextView addMe1, addMe2, addMe3, addMe4, removeMe1, removeMe2, removeMe3, removeMe4;
LinearLayout editMessage;
EditText helpMessage;
Button saveState;
EditText delayTimer;
TextView secTV;
ImageView smsSettingsBack;
SharePrefManager SM;
在 onCreate 里面:
EditText phoneNumber1, phoneNumber2, phoneNumber3, phoneNumber4, phoneNumber5;
LinearLayout removeable1, removeable2, removeable3, removeable4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub11
super.onCreate(savedInstanceState);
setContentView(R.layout.quick_sms_settings);
SM = new SharePrefManager(getApplicationContext());
smsSettingsBack = (ImageView)findViewById(R.id.smsSettingsBack);
phoneNumber1 = (EditText)findViewById(R.id.phoneNumber1);
phoneNumber2 = (EditText)findViewById(R.id.phoneNumber2);
phoneNumber3 = (EditText)findViewById(R.id.phoneNumber3);
phoneNumber4 = (EditText)findViewById(R.id.phoneNumber4);
phoneNumber5 = (EditText)findViewById(R.id.phoneNumber5);
removeable1 = (LinearLayout)findViewById(R.id.removeable1);
removeable2 = (LinearLayout)findViewById(R.id.removeable2);
removeable3 = (LinearLayout)findViewById(R.id.removeable3);
removeable4 = (LinearLayout)findViewById(R.id.removeable4);
addMe1 = (TextView)findViewById(R.id.addMe1);
addMe2 = (TextView)findViewById(R.id.addMe2);
addMe3 = (TextView)findViewById(R.id.addMe3);
addMe4 = (TextView)findViewById(R.id.addMe4);
removeMe1 = (TextView)findViewById(R.id.removeMe1);
removeMe2 = (TextView)findViewById(R.id.removeMe2);
removeMe3 = (TextView)findViewById(R.id.removeMe3);
removeMe4 = (TextView)findViewById(R.id.removeMe4);
secTV = (TextView)findViewById(R.id.secTV);
Utils.TypeFaceLight(secTV, getAssets());
delayTimer = (EditText)findViewById(R.id.delayTimer);
String timerTime = String.valueOf(SM.getTimer()/1000);
delayTimer.setText(timerTime);
Utils.TypeFaceET(delayTimer, getAssets());
saveState = (Button)findViewById(R.id.saveNumbers);
Utils.TypeFaceButton(saveState, getAssets());
helpMessage = (EditText)findViewById(R.id.helpMessage);
String message = SM.getHelpMessage();
helpMessage.setText(message);
smsSettingsBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
if(!SM.getValue(SM.KEY_NUMBER1).equalsIgnoreCase("")){
phoneNumber1.setText(SM.getValue(SM.KEY_NUMBER1));
}else{
phoneNumber1.setText("");
}
if(!SM.getValue(SM.KEY_NUMBER2).equalsIgnoreCase("")){
phoneNumber2.setText(SM.getValue(SM.KEY_NUMBER2));
addLayout(removeable1);
removeTextView(addMe1);
}else{
phoneNumber2.setText("");
}
if(!SM.getValue(SM.KEY_NUMBER3).equalsIgnoreCase("")){
phoneNumber3.setText(SM.getValue(SM.KEY_NUMBER3));
addLayout(removeable2);
removeTextView(addMe2);
}else{
phoneNumber3.setText("");
}
if(!SM.getValue(SM.KEY_NUMBER4).equalsIgnoreCase("")){
phoneNumber4.setText(SM.getValue(SM.KEY_NUMBER4));
addLayout(removeable3);
removeTextView(addMe3);
}else{
phoneNumber4.setText("");
}
if(!SM.getValue(SM.KEY_NUMBER5).equalsIgnoreCase("")){
phoneNumber5.setText(SM.getValue(SM.KEY_NUMBER5));
addLayout(removeable4);
removeTextView(addMe4);
}else{
phoneNumber5.setText("");
}
Log.i("PHONE 1", phoneNumber1.getText().toString());
Log.i("PHONE 2", phoneNumber2.getText().toString());
saveState.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(phoneNumber1.getText().toString().length()<2){
Toast.makeText(getApplicationContext(), "Please set number to save.", Toast.LENGTH_LONG).show();
}else{
String number1 = phoneNumber1.getText().toString();
String number2 = phoneNumber2.getText().toString();
String number3 = phoneNumber3.getText().toString();
String number4 = phoneNumber4.getText().toString();
String number5 = phoneNumber5.getText().toString();
String message = helpMessage.getText().toString();
String timer = delayTimer.getText().toString();
SM.setValue(SM.KEY_NUMBER1, number1);
SM.setValue(SM.KEY_NUMBER2, number2);
SM.setValue(SM.KEY_NUMBER3, number3);
SM.setValue(SM.KEY_NUMBER4, number4);
SM.setValue(SM.KEY_NUMBER5, number5);
SM.setValue(SM.KEY_HELPMESSAGE, message);
SM.setTimer(timer);
Toast.makeText(getApplicationContext(), "Your settings has been saved", Toast.LENGTH_LONG).show();
}
}
});
addMe1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addLayout(removeable1);
removeTextView(addMe1);
}
});
addMe2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addLayout(removeable2);
removeTextView(addMe2);
}
});
addMe3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addLayout(removeable3);
removeTextView(addMe3);
}
});
addMe4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addLayout(removeable4);
removeTextView(addMe4);
}
});
removeMe1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeLayout(removeable1);
addTextView(addMe1);
Log.i("NO2",phoneNumber2.getText().toString());
if(phoneNumber2.getText().toString().length()>0)
phoneNumber2.setText("");
SM.removeValue(SM.KEY_NUMBER2);
}
});
removeMe2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeLayout(removeable2);
addTextView(addMe2);
if(phoneNumber3.getText().toString().length()>0){
phoneNumber3.setText("");
}
SM.removeValue(SM.KEY_NUMBER3);
}
});
removeMe3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeLayout(removeable3);
addTextView(addMe3);
if(phoneNumber4.getText().toString().length()>0)
phoneNumber4.setText("");
SM.removeValue(SM.KEY_NUMBER4);
}
});
removeMe4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeLayout(removeable4);
addTextView(addMe4);
if(phoneNumber5.getText().toString().length()>0)
phoneNumber5.setText("");
SM.removeValue(SM.KEY_NUMBER5);
}
});
}
public void addLayout(LinearLayout layout){
layout.setVisibility(View.VISIBLE);
}
public void removeLayout(LinearLayout layout){
layout.setVisibility(View.GONE);
}
public void removeTextView(TextView tv){
tv.setVisibility(View.GONE);
}
public void addTextView(TextView tv){
tv.setVisibility(View.VISIBLE);
}
日志条目:
09-07 15:59:10.866: E/AndroidRuntime(7853): FATAL EXCEPTION: main
09-07 15:59:10.866: E/AndroidRuntime(7853): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vaw.selfhelp/com.vaw.selfhelp.SMSOptions}: java.lang.NullPointerException
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.os.Looper.loop(Looper.java:153)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread.main(ActivityThread.java:4987)
09-07 15:59:10.866: E/AndroidRuntime(7853): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 15:59:10.866: E/AndroidRuntime(7853): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 15:59:10.866: E/AndroidRuntime(7853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-07 15:59:10.866: E/AndroidRuntime(7853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-07 15:59:10.866: E/AndroidRuntime(7853): at dalvik.system.NativeStart.main(Native Method)
09-07 15:59:10.866: E/AndroidRuntime(7853): Caused by: java.lang.NullPointerException
09-07 15:59:10.866: E/AndroidRuntime(7853): at com.vaw.selfhelp.SMSOptions.onCreate(SMSOptions.java:107)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.Activity.performCreate(Activity.java:5020)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-07 15:59:10.866: E/AndroidRuntime(7853): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-07 15:59:10.866: E/AndroidRuntime(7853): ... 11 more
第 107 行:
else{
phoneNumber2.setText("");// 107
}
我在我的日志调用中得到了 NPE。为什么会这样?