簡體   English   中英

從片段類訪問非活動類中的共享首選項

[英]Accessing shared preferences in a non-activity class from fragment class

我有一個片段:

public class TodayVerse extends Fragment
{
    TextView textView;

    DailyQuranMethods dailyQuranMethods;

    public TodayVerse() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_today_verse, container, false);
        textView = (TextView) view.findViewById(R.id.verse);
//        setChapterVerse();

        textView.setText(dailyQuranMethods.getVerseToday(dailyQuranMethods.DateToday(),getActivity().getApplicationContext()) + "\nChapter:" + dailyQuranMethods.getChapterTodayName(dailyQuranMethods.DateToday(),getActivity().getApplicationContext()));
        return view;
    }
}

該片段調用一個非活動類: dailyQuranMethods ,該類使用sharedPrefrences。 為此,我必須將Context作為參數傳遞。 我通過了:

getActivity()。getApplicationContext()

dailyQuranMethods中的方法如下:

   public String getVerseToday(String today,Context context){
...
}

在運行該應用程序時,我有以下logcat:

06-19 06:07:56.587 9887-9887 /? E / AndroidRuntime致命錯誤:com.example.shiza.dailyquranverses.TodayVerse.onCreateView(TodayVerse.java:35)處的主要java.lang.NullPointerException android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) ),位於android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138),位於android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955),位於android.support.v4.app.BackStackRecord。在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)運行(BackStackRecord.java:740) .v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)在android.view.View.dispatchAttachedToWindow(View.java:9788)在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2198)在android.view。位於android.view.ViewGroup.dispatchAttachedToWin的ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206) 在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)處的dow(ViewGroup.java:2206)在android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java)處的android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)處的dow :2206),位於android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:971),位於android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467),位於android.os.Handler.dispatchMessage(Handler.java:99) .os.Looper.loop(Looper.java:137)在android.app.ActivityThread.main(ActivityThread.java:4424)在java.lang.reflect.Method.invokeNative(本機方法)在java.lang.reflect.Method .com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)位於com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)位於.invoke(Method.java:511) dalvik.system.NativeStart.main(本機方法)

錯誤在以下行中:

textView.setText(dailyQuranMethods.getVerseToday(dailyQuranMethods.DateToday(),getActivity()。getApplicationContext())+“ \\ nChapter:” + dailyQuranMethods.getChapterTodayName(dailyQuranMethods.DateToday(),getActivity()。getApplicationContext());

看起來,我無法在非活動類中獲取Context。 我嘗試了很多,但沒有任何幫助。 請幫我解決這個問題。

編輯1:非活動類是:

package com.example.shiza.dailyquranverses;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.MatrixCursor;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.widget.SimpleCursorAdapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import com.example.shiza.dailyquranverses.TodayChapter;
import android.preference.Preference;

/**
 * Created by Shiza on 19-06-2015.
 */
public class DailyQuranMethods {

    //    generate a random number.
    SharedPreferences sharedPreferencesChapter;
    SharedPreferences sharedPreferencesVerse;
    private static String TODAY_CHAPTER = "TODAY_CHAPTER";
    private static String TODAY_VERSE = "TODAY_VERSE";
    Context context;




    public int GetRandom(int min, int max) {
        Random ran = new Random();
        return ran.nextInt((max - min) + 1) + min;
    }


    public void setChapterVerseOfToday(Context context)
    {

        sharedPreferencesChapter = context.getSharedPreferences(TODAY_CHAPTER, Context.MODE_PRIVATE);
        sharedPreferencesVerse = context.getApplicationContext().getSharedPreferences(TODAY_VERSE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor_chapter = sharedPreferencesChapter.edit();
        SharedPreferences.Editor editor_verse = sharedPreferencesVerse.edit();


        int chapter_no = GetRandom(1, 114);

        String chapter_array_name = "chapter_" + chapter_no;

        String verse = sharedPreferencesVerse.getString(DateToday(),null);

        if ( verse == null )
        {
            editor_verse.putString(DateToday(), getVerse(context,chapter_array_name));
            editor_verse.apply();
        }

        int chapter_number = sharedPreferencesChapter.getInt(DateToday(),0);

        if ( chapter_number == 0 )
        {

            editor_chapter.putInt(DateToday(),chapter_no);
            editor_chapter.apply();
        }


    }
    public String getVerseToday(String today,Context context)
    {
        sharedPreferencesVerse = context.getSharedPreferences(TODAY_VERSE, Context.MODE_PRIVATE);
        return sharedPreferencesVerse.getString(today,"7. The Way of those on whom You have bestowed Your Grace, not (the way) of those who earned Your Anger (such as the Jews), nor of those who went astray (such as the Christians).");
    }

    public String getVerse(Context context,String chapter_array_name)
    {
        int id = context.getResources().getIdentifier(chapter_array_name, "array", context.getPackageName());
        String[] chapter = context.getResources().getStringArray(id);
        int random_verse = GetRandom(1, chapter.length - 1);
        return chapter[random_verse];
    }


    public String[] getChapterTodayContent(String today,Context context)
    {
        sharedPreferencesChapter = context.getSharedPreferences(TODAY_CHAPTER, Context.MODE_PRIVATE);
        int chapter_no = sharedPreferencesChapter.getInt(today, 2);
        String chapter_array_name = "chapter_" + chapter_no;
        int id = context.getResources().getIdentifier(chapter_array_name, "array", context.getApplicationContext().getPackageName());
        return context.getResources().getStringArray(id);
    }

    public String getChapterTodayName(String today,Context context)
    {
        sharedPreferencesChapter = context.getApplicationContext().getSharedPreferences(TODAY_CHAPTER,Context.MODE_PRIVATE);
        int chapter_no = sharedPreferencesChapter.getInt(today, 0);
        String[] chapterName = context.getResources().getStringArray(R.array.chapters);
        return chapterName[chapter_no - 1];
    }



    public String DateToday()
   {
       Calendar c = Calendar.getInstance();

       SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
       return df.format(c.getTime());
   }


    //    Get quran verses in android
    public String[] getQuranVerses(Context context) {
        String[] whole_quran = new String[7000];
        String[] current_chapter;
        String chapterSize = "";
        String[] chapter_names;
        String chapter_array_name;
        int total_verse = 0, verse_in_current_chapter, chapter_number;

        chapter_names = context.getResources().getStringArray(R.array.chapters);

        for (chapter_number = 1; chapter_number < 114; chapter_number++) {
//            Grab each chapter containing verse from Quran
            chapter_array_name = "chapter_" + chapter_number;
            int id = context.getResources().getIdentifier(chapter_array_name, "array", context.getPackageName());
            current_chapter = context.getResources().getStringArray(id);

            for (verse_in_current_chapter = 1; verse_in_current_chapter < current_chapter.length - 1; verse_in_current_chapter++) {
                whole_quran[total_verse] = current_chapter[verse_in_current_chapter] + "," + chapter_names[chapter_number - 1];
                total_verse++;
            }
            chapterSize += chapter_number + ":" + chapter_names[chapter_number - 1] + ":" + current_chapter.length + "\n";
        }
        return whole_quran;

    }

    //    public void search()
//    {
//        //      Use search view on the top of your app
//
//        search = (SearchView) findViewById(R.id.mySearchView);
//        search.setQueryHint("Search Qur'an");
//
////      create a suggestion adapter for dropdown
//        final String[] from = new String[] {"cityName"};
//        final int[] to = new int[] {android.R.id.text1};
//        mAdapter = new SimpleCursorAdapter(this,
//                android.R.layout.simple_list_item_2,
//                null,
//                from,
//                to,
//                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//
//        search.setSuggestionsAdapter(mAdapter);
//
//        search.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
//            @Override
//            public boolean onSuggestionClick(int position) {
//                // Your code here
//
//
//                Cursor theCursor = (Cursor) mAdapter.getCursor();
//                String selectedItem = theCursor.getString(position);
////                Toast.makeText(getBaseContext(), " on suggestion click position and item is" + position + selectedItem, Toast.LENGTH_LONG).show();
//
//
//                startActivity(new Intent(getBaseContext(), MainActivity.class));
//
//                return true;
//            }
//
//            @Override
//            public boolean onSuggestionSelect(int position) {
//                // Your code here
////                Toast.makeText(getBaseContext(), " on suggestion select position is" + position, Toast.LENGTH_LONG).show();
//
//                startActivity(new Intent(getBaseContext(), MainActivity.class));
//
//                return true;
//            }
//        });
//
//        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//
//
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                startActivity(new Intent(getBaseContext(), MainActivity.class));
//
//                return false;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String newText) {
//                //
//                dailyQuranMethods.populateAdapter(newText,getApplicationContext());
//
//                return false;
//            }
//
//
//        });
//
//
//
//    }


    public void populateAdapter(String query, Context context, SimpleCursorAdapter mAdapter) {
        String[] SUGGESTIONS = getQuranVerses(context);
        final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "cityName"});
        int j = 0;
        int k = 0;
        if (query.length() > 3) {
            k = GetRandom(0, 60);
//            Toast.makeText(getApplicationContext(),"K is " + k,Toast.LENGTH_LONG).show();
            for (int i = 0; i < 6144; i++) {

                if (SUGGESTIONS[i].toLowerCase().contains(query.toLowerCase()) && SUGGESTIONS[i].length() > 0) {
                    c.addRow(new Object[]{i, SUGGESTIONS[i]});
                    j++;

                    if (j > 100) {
                        break;
                    }
                }
            }
            if (j == 0) {
                c.addRow(new Object[]{0, "No results found."});
            }
        } else {
            c.addRow(new Object[]{0, "Please enter at least 3 characters."});
            c.addRow(new Object[]{1, "Please be patient, we have to find in more than 6,000 verses"});
        }


        mAdapter.changeCursor(c);
    }

}

調用getActivity ()很好,它將解析為一個Context。 您不需要完整的getActivity().getApplicationContext() 這可能會導致問題,因為您要告訴Android使用應用程序的上下文而不是當前的Activity。 總有一天你會感激的。

除此之外,您還需要創建dailyQuranMethods的實例 在Java中,除非對象是static否則始終需要使用new進行創建。 記住這一點很好。

代碼示例:

public TodayVerse() {
    DailyQuranMethods dailyQuranMethods = new DailyQuranMethods();
}

Android Java祝您好運...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM