簡體   English   中英

如何在Android中從另一個類調用一個方法

[英]How to call one method from another class in Android

每當我即時調用fromcheckpass(mcontext)我得到nullpointerexception。 哪里做錯了。 幫幫我!

onClick()我正在調用fromcheckpass(mcontext)

public void onClick(View v) {
    InboxActivity inboxActivity = new InboxActivity();
    inboxActivity.fromcheckpass(CheckPass.this);
}

如何調用fromcheckpass(mcontext)方法?

public void fromcheckpass(Context mcontext) {
    Toast.makeText(mcontext, "WEL COME", Toast.LENGTH_LONG).show();
    Db = new MySQLiteHelper(this);
    String DbInsert = Utils.getPreferences("DbInsert", this);

    if (!DbInsert.equalsIgnoreCase("Inserted")) {
        SaveDataInDB();
    }

    MessageListView = (ListView) findViewById(R.id.lvInbox);
    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new    ColorDrawable(Color.parseColor("#0154A4")));
    bar.setTitle(R.string.app_name);
    bar.setTitle(Html.fromHtml("<font color='#ffffff'>WeText </font>"));
    bar.setIcon(R.drawable.icon_top);

    Utils.getOverflowMenu(this);

    attachListeners();

    dataList = Utils.getLatestMessageOfAllContacts(InboxActivity.this);

    if (dataList.isEmpty()) {
        Toast.makeText(getApplicationContext(), "NO MESSAGES", Toast.LENGTH_LONG).show();
    } else {
        iAdapter = new Adapter(InboxActivity.this, dataList);
        MessageListView.setAdapter(iAdapter);
    }
}

每當我調用fromCheckpass(mcontex)方法時,應用程序就會崩潰。 這是我的logcat:

Process: com.futureappspk.WeTextFree, PID: 29065
    java.lang.NullPointerException
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:186)
        at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
        at com.futureappspk.WeTextFree.Utils.getPreferences(Utils.java:432)
        at com.futureappspk.WeTextFree.InboxActivity.fromcheckpass(InboxActivity.java:130)
        at com.futureappspk.WeTextFree.CheckPass$1.onClick(CheckPass.java:40)

這是我的util類方法,它正在調用fromcheckpass(mcontext)

public static String getPreferences(String key, Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String userName = sharedPreferences.getString(key, "UserName");
    return userName;
}

請嘗試這個
我認為您的CheckPass不是您的活動。
因此正確傳遞上下文
getActivity()或getApplicationContext();
在onClick方法中希望對您有所幫助

通知您錯誤在此行

String DbInsert = Utils.getPreferences("DbInsert", this);

可能的原因

您必須將getSharedPreferencesContext類的實例一起使用,例如

ctx.getSharedPreferences("MY_PREF",0);

您尚未顯示您的Utils類,但正如我猜想的那樣, getPreferences方法是該類的靜態方法,您可以直接調用它而無需初始化任何應用程序Context 因此,我建議您在Utils類的Constructor中初始化Context ,並僅將getSharedPreferences方法與Utils類的實例一起使用。

UPDATE

盡量不要使其變為靜態,做這樣的事情

public class Utils {
private SharedPreferences sharedPreferences; 

public Utils (Context context) {
   this.sharedPreferences = getPreferences(MODE_PRIVATE);    
}
public String getPreferences(String key) {
   sharedPreferences.getString(key, "UserName");
    return userName;
  }
}

暫無
暫無

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

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