我正在尝试创建一个内部通信应用,但是当我尝试运行它时,它会一直给我一个< Inventory id: nil, amt_sold: 175.0>
的错误。
任何人都可以帮我这个吗?
getContext() is null
这是我的logcat
public class intercomApp extends Application {
protected static final String SHARED_PREFS_REFERENCE_LABEL = "ComeNChat";
protected static final String DEFAULT_NAME = "Default_Name";
private static intercomApp instance;
public static intercomApp getInstance() {
return instance;
}
public static Context getContext() {
return instance;
}
@Override
public void onCreate() {
instance = this;
super.onCreate();
}
// store our name to shared prefs
public static void storeName(String name) {
PreferenceManager.getDefaultSharedPreferences(getContext()).edit().putString(SHARED_PREFS_REFERENCE_LABEL, name).commit();
}
// get our name from shared prefs
public static String getName() {
return PreferenceManager.getDefaultSharedPreferences(getContext()).getString(SHARED_PREFS_REFERENCE_LABEL, DEFAULT_NAME); //(intercomApp.java:45)
}
虽然这是MainActivity中使用的功能
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricky.comenchat/com.example.ricky.comenchat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
at com.example.ricky.comenchat.util.intercomApp.getName(intercomApp.java:45)
at com.example.ricky.comenchat.MainActivity.getClientNameList(MainActivity.java:383)
at com.example.ricky.comenchat.MainActivity.setupClientListAdapter(MainActivity.java:214)
at com.example.ricky.comenchat.MainActivity.onCreate(MainActivity.java:149)
at android.app.Activity.performCreate(Activity.java:6251)
EDITED: 下面是我的manifest.xml
public class MainActivity extends AppCompatActivity {
@Overide
protected void onCreate(Bundle savedInstanceState){
setupList; //(MainActivity.java:149)
}
private void setupList{
nameList = getNamelist(); //(MainActivity.java:214)
}
private String[] getNamelist(){
ArrayList<String> names = new ArrayList<>();
names.add(intercomApp.getName()); //(MainActivity.java:383)
}
答案 0 :(得分:1)
您的Manifest中似乎缺少Application,因此Context为null
<application
android:name=".util.intercomApp"
android:allowBackup="true"
旁注:Java约定 - 类名称应以大写字母
开头答案 1 :(得分:-1)
而不是像这样创造一个瞬间
public static intercomApp getInstance() {
return instance;
}
public static Context getContext() {
return instance;
}
您可以执行以下操作,并使IntercomApp类具有通用性,如下所示
public class IntercomApp {
protected static final String SHARED_PREFS_REFERENCE_LABEL = "ComeNChat";
protected static final String DEFAULT_NAME = "Default_Name";
// store our name to shared prefs
public static void storeName(String name, Context context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(SHARED_PREFS_REFERENCE_LABEL, name).commit();
}
// get our name from shared prefs
public static String getName(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(SHARED_PREFS_REFERENCE_LABEL, DEFAULT_NAME); //(intercomApp.java:45)
}
}
并在您的任何活动中使用它,如下所示
public class MainActivity extends AppCompatActivity {
private Context context;
@Overide
protected void onCreate(Bundle savedInstanceState){
context = this;
setupList(); //(MainActivity.java:149)
}
private void setupList() {
nameList = getNamelist(); //(MainActivity.java:214)
}
private String[] getNamelist(){
ArrayList<String> names = new ArrayList<>();
names.add(IntercomApp.getName(context)); //(MainActivity.java:383)
}
}