使用getRessources()从string.xml获取值时出错。
这是我的档案:
import android.app.Activity;
import android.util.Log;
import android.content.Context;
public class mailing extends Activity {
int[] mail_images= new int[]{0, R.drawable.simple, R.drawable.p};
String[] mail_array_spinner = new String[]{getResources().getString(R.string.type_select),getResources().getString(R.string.maillist)};
public int get_image_id(String check){
int got=-1;
for(int i=0;i<mail_images.length;i++){
if(mail_array_spinner[i].equals(check)){
got=i;
}
}
return mail_images[got];
}
}
我的应用程序在读取代码时停止,错误来自:
String[] mail_array_spinner = new String[]{getResources().getString(R.string.type_select),getResources().getString(R.string.maillist)};
这里是日志:
01-02 16:59:39.255 6087-6087/com.example.xx.cxeapp D/typei: mail
01-02 16:59:39.257 6087-6087/com.example.xx.cxeapp D/AndroidRuntime: Shutting down VM
01-02 16:59:39.258 6087-6087/com.example.xx.cxeapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xx.cxeapp, PID: 6087
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at com.example.mouh.myapplication.mail.<init>(mail.java:11)
at Adapters.InProgressListAdapter.getView(InProgressListAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1489)
at android.view.View.measure(View.java:18788)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
有任何帮助来解决这个问题吗?感谢。
答案 0 :(得分:1)
您尚未在onCreate()
内编写代码,请执行以下操作
public class mailing extends Activity {
int[] mail_images;
String[] mail_array_spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mail_images= new int[]{0, R.drawable.simple, R.drawable.p};
mail_array_spinner = new String[]{getResources().getString(R.string.type_select),getResources().getString(R.string.maillist)};
}
public int get_image_id(String check){
int got=-1;
mail_images= new int[]{0, R.drawable.simple, R.drawable.p};
mail_array_spinner = new String[]{getResources().getString(R.string.type_select),getResources().getString(R.string.maillist)};
for(int i=0;i<mail_images.length;i++){
if(mail_array_spinner[i].equals(check)){
got=i;
}
}
return mail_images[got];
}
}
答案 1 :(得分:1)
您正在尝试在创建活动之前调用getResources(),这会导致空指针异常。在onCreate()方法中调用getResources。
fh.seek(0,2)