且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

越来越"不幸的是已经停止"使用资源时出错

更新时间:2022-10-15 11:20:33

请来电的getResource在onCreate()方法。

I think I don't need to tell what I try to but I want to tell. There is 2 <string-array> first is for android:entries second for values of these entries. When user selects the item I want to get value of that item.

Also I want to ask, When application is opened, dialog run. I want to make it run just when after user select an item.

public class Select extends Activity implements OnItemSelectedListener{

    Resources rsc = getResources();
    @SuppressLint("Recycle")
    final TypedArray itemValues = rsc.obtainTypedArray(R.array.selectValues);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);

        Spinner form = (Spinner) findViewById(R.id.sSelect);
        form.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> item, View arg1, int sort,
            long arg3) {
        // TODO Auto-generated method stub
        int selectedItem = itemValues.getInt(sort, 1);
        Dialog d = new Dialog(this);
        TextView t = new TextView(this);
        t.setText(selectedItem);
        d.setContentView(t);
        d.setTitle("Sonuc!");
        d.show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }



}

Problem appear when I use Resources. Also logcat is here. What's the problem and solution of this problem?

EDIT: Also here is the array.xml

<resources>

    <string-array name="select">
        <item>Strength</item>
        <item>Size</item>
        <item>Fat loss</item>
    </string-array>

    <integer-array name="selectValues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </integer-array>

</resources>

please call getResource in onCreate() method.