Android を初めて使用し、共有設定に基づいて view.GONE を設定しようとしています。しかし、それはなくなっていません!私は何を壊した?!
編集だから、下の問題をテストした後、設定が正しく行われていないということでした...それが修正されたので、もう一度試してみましょう。彼は私のonCreateです...テキストビューの一時コードを削除すると、正常に機能し、トーストを介して設定の変更が表示されますが、コードがそこにあるとアプリがクラッシュします。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
// Inflate the layout for this fragment
Boolean symptothermal = mPreferences.getBoolean("symptothermal", true);
if (!symptothermal) {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show();
} else {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show();
}
if (!symptothermal) {
TextView temp = (TextView) getView().findViewById(R.id.temp);
temp.setVisibility(View.GONE);
}
}
彼は XML FYI です。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.23" >
<!-- Date Text -->
<EditText
android:id="@+id/dateselected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/pickdate"
android:gravity="center"
android:inputType="date" />
<!-- DatePicker button -->
<Button
android:id="@+id/pickdate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dateselected"
android:layout_alignBottom="@+id/dateselected"
android:layout_alignParentRight="true"
android:src="@drawable/ic_datepicker"
android:text="@string/date" />
<!-- Temp Label -->
<EditText
android:id="@+id/temp"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/pickdate"
android:ems="10"
android:inputType="text"
android:text="@string/temp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="10dp">
</EditText>
<!-- Temp field -->
<EditText
android:id="@+id/tempvalue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="@+id/pickdate"
android:layout_toRightOf="@+id/dateselected"
android:ems="10"
android:inputType="number"
android:layout_marginTop="10dp" />
</RelativeLayout>
<!-- Notes Field -->
<EditText
android:id="@+id/chartingnote"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.58"
android:ems="20"
android:gravity="top|center_horizontal"
android:hint="@string/hintNote"
android:inputType="textMultiLine" />
<!-- Update Button -->
<Button
android:id="@+id/chartingupdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/charting_update" />
</LinearLayout>
EDIT @ daniel_c05:
package com.projectcaruso.naturalfamilyplaning;
import com.projectcaruso.naturalfamilyplanning.R;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class ChartingFragment extends Fragment implements OnDateSetListener {
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Boolean symptothermal = mPreferences.getBoolean("symptothermal", true);
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
}
public void showDatePickerDialog(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getChildFragmentManager(), "datePicker");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!symptothermal) {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 1!", Toast.LENGTH_LONG).show();
} else {
Context context = getActivity().getApplicationContext();
Toast.makeText(context, "Hello toast 2!", Toast.LENGTH_LONG).show();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate a view
View view = inflater.inflate(R.layout.fragment_charting, null);
//now you can initialize the TextView
//notice how we don't call getView()? :P
TextView temp = (TextView) view.findViewById(R.id.temp);
if (!symptothermal) {
//Hide the view if necessary
temp.setVisibility(View.GONE);
}
return view;
}
}
ログ:
05-08 14:10:55.042: E/AndroidRuntime(22900): FATAL EXCEPTION: main
05-08 14:10:55.042: E/AndroidRuntime(22900): java.lang.NullPointerException
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
05-08 14:10:55.042: E/AndroidRuntime(22900): at com.projectcaruso.naturalfamilyplaning.ChartingFragment.<init>(ChartingFragment.java:21)
05-08 14:10:55.042: E/AndroidRuntime(22900): at com.projectcaruso.naturalfamilyplaning.MenuFragment.onListItemClick(MenuFragment.java:36)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.widget.AbsListView$1.run(AbsListView.java:3423)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Handler.handleCallback(Handler.java:725)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.os.Looper.loop(Looper.java:137)
05-08 14:10:55.042: E/AndroidRuntime(22900): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 14:10:55.042: E/AndroidRuntime(22900): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 14:10:55.042: E/AndroidRuntime(22900): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 14:10:55.042: E/AndroidRuntime(22900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 14:10:55.042: E/AndroidRuntime(22900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 14:10:55.042: E/AndroidRuntime(22900): at dalvik.system.NativeStart.main(Native Method)