[英]How to call one method from another class in Android
When ever im calling fromcheckpass(mcontext)
im getting nullpointerexception. 每当我即时调用fromcheckpass(mcontext)
我得到nullpointerexception。 Where imdoing wrong. 哪里做错了。 Help me out! 帮帮我!
From onClick()
i'm calling fromcheckpass(mcontext)
. 从onClick()
我正在调用fromcheckpass(mcontext)
。
public void onClick(View v) {
InboxActivity inboxActivity = new InboxActivity();
inboxActivity.fromcheckpass(CheckPass.this);
}
How can I call fromcheckpass(mcontext)
method ? 如何调用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);
}
}
Whenever I call fromCheckpass(mcontex) method the app crashes. 每当我调用fromCheckpass(mcontex)方法时,应用程序就会崩溃。 Here is my logcat: 这是我的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)
Here is my util class method which is calling fromcheckpass(mcontext)
这是我的util类方法,它正在调用fromcheckpass(mcontext)
public static String getPreferences(String key, Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String userName = sharedPreferences.getString(key, "UserName");
return userName;
}
Please try This 请尝试这个
I think your CheckPass is not your Activity. 我认为您的CheckPass不是您的活动。
Hence pass Context correctly like 因此正确传递上下文
getActivity() or getApplicationContext(); getActivity()或getApplicationContext();
in onClick Method Hope it helps 在onClick方法中希望对您有所帮助
To inform you the error is at this line 通知您错误在此行
String DbInsert = Utils.getPreferences("DbInsert", this);
Possible reasons 可能的原因
You have to use getSharedPreferences
with the instance of Context
class like 您必须将getSharedPreferences
与Context
类的实例一起使用,例如
ctx.getSharedPreferences("MY_PREF",0);
You have not shown your Utils
class but as I guess getPreferences
method is a static method of the class and you are calling it directly without initializing any Context
of application. 您尚未显示您的Utils
类,但正如我猜想的那样, getPreferences
方法是该类的静态方法,您可以直接调用它而无需初始化任何应用程序Context
。 So I suggest you to initialize the Context
in the Constructor
of your Utils
class and use the getSharedPreferences
method only with the instance of Utils
class. 因此,我建议您在Utils
类的Constructor
中初始化Context
,并仅将getSharedPreferences
方法与Utils
类的实例一起使用。
UPDATE UPDATE
Try not to make it static, do something like this 尽量不要使其变为静态,做这样的事情
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.