android.content.res.Resources $ NotFoundException:onCreate()之后的资源ID#0x0

时间:2013-09-09 15:22:58

标签: android xml

我正在开发一款Android应用。我有这个xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeaveParamsActivity" >

<Button
    android:id="@+id/sendparkalertbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/accept" />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/sendparkalertbutton"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                            <TextView
            android:id="@+id/leaveparamsheader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/parkalertparams"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/parkalertsparamshere"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/leaveparamsheader"
            android:gravity="center"
            android:text="@string/parkalertparamshere"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <RadioGroup
            android:id="@+id/radioGroupLeave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/parkalertsparamshere" >

            <RadioButton
                android:id="@+id/radioleave0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/yes" />

            <RadioButton
                android:id="@+id/radioleave1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/no" />
        </RadioGroup>

        <TimePicker
            android:id="@+id/timePickerLeave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/parkalertparamstimetext" />

        <TextView
            android:id="@+id/parkalertsparamsadress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/radioGroupLeave"
            android:layout_toRightOf="@+id/radioGroupLeave"
            android:gravity="center"
            android:text="@string/parkalertparamsadress"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/parkalertadress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/radioGroupLeave"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/radioGroupLeave"
            android:layout_marginLeft="10dp"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/parkalertparamstimetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/radioGroupLeave"
            android:gravity="center"
            android:text="@string/parkalertparamstime"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/waittimetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/timePickerLeave"
            android:layout_centerHorizontal="true"
            android:text="@string/waittime"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/waittimetext" />

            </RelativeLayout>
        </ScrollView>

</RelativeLayout>

这是使用它的Activity中的onCreate()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.leaveparamslayout);
    c=getApplicationContext();
    for (int i=0;i<16;i++){
        oneto15.add(i,i);
    }
    bundle=getIntent().getExtras();
    username=bundle.getString("username");
    hereRadioButtonBoolean=(RadioGroup)findViewById(R.id.radioGroupLeave);//existe
    parkadress=(EditText)findViewById(R.id.parkalertadress);//existe
    picker=(TimePicker)findViewById(R.id.timePickerLeave);//existe
    spinner=(Spinner)findViewById(R.id.spinner1);//existe
    textToHide=(TextView)findViewById(R.id.parkalertsparamsadress);//existe
    send=(Button)findViewById(R.id.sendparkalertbutton);//existe
    ocultarBotones();
    hereRadioButtonBoolean.check(R.id.radioleave0);
    rbno=(RadioButton)findViewById(R.id.radioleave1);
    LocationManager locationManager;
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            5000, 0, this);
    lastKnownLocation = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    rbno.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked==true){

                mostrarBotones();
            }else{
                ocultarBotones();
                parkadress.setText("");
            }

        }




    });


    ArrayAdapter adapter=new ArrayAdapter(c, 0, oneto15);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setSelection(0);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            minsToWait=Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }


    });
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Calendar today = Calendar.getInstance();
            today.set(Calendar.MILLISECOND, 0);
            today.set(Calendar.SECOND, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.HOUR_OF_DAY, 0);
            hoyalasdoce=today.getTimeInMillis();
            horas=picker.getCurrentHour();
            minutos=picker.getCurrentMinute();
            try {
                parkdate=new SimpleDateFormat("dd/MM/yyyy").parse(String.valueOf(horas)+String.valueOf(minutos));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            parklong=parkdate.getTime()+hoyalasdoce;

            if(rbno.isChecked()){
                parsearDireccion(parkadress.getText().toString());
                if(lat==0){
                    Toast toast=Toast.makeText(c, getString(R.string.badparse), Toast.LENGTH_LONG);
                    toast.show();
                    haveAdress=false;
                }else{
                    haveAdress=true;
                }

            }else{

                if(lastKnownLocation!=null){
                    lat=lastKnownLocation.getLatitude();
                    lon=lastKnownLocation.getLongitude();
                }else{
                    dialog = ProgressDialog.show(c, "Buscando...", "");
                    handler = new Handler() {

                        @Override
                        public void handleMessage(Message msg) {
                            Log.i("Location", "Mensaje recibido");
                            if(dialog!=null){
                                Log.i("Location", "dialog no es null");
                            dialog.dismiss();
                            }
                            Log.i("Location", "Coordenadas recibidas en el handler: Longitud: "+msg.getData().getDouble("lat")+" Latitud: "+msg.getData().getDouble("lon"));
                            LatLng parkspot = new LatLng(msg.getData().getDouble("lat"),
                                    msg.getData().getDouble("lon"));


                        }
                    };
                }
            }

            Thread thread=new Thread(){
                public void run(){
                    urlsend=getString(R.string.host)+getString(R.string.namespace)+getString(R.string.urlsendpark);
                    params.clear();
                    params.add(new BasicNameValuePair("lat", String.valueOf(lat)));
                    params.add(new BasicNameValuePair("lon", String.valueOf(lon)));
                    params.add(new BasicNameValuePair("time", String.valueOf(parklong)));
                    params.add(new BasicNameValuePair("wait", String.valueOf(minsToWait)));
                    try {
                        CustomHttpClient.executeHttpPost(urlsend, params);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
        }
    });





}

调用此Activity时,我会收到此堆栈跟踪:

09-09 17:12:19.621: E/AndroidRuntime(10627): FATAL EXCEPTION: main
09-09 17:12:19.621: E/AndroidRuntime(10627): android.content.res.Resources$NotFoundException: Resource ID #0x0
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.content.res.Resources.getValue(Resources.java:1018)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.content.res.Resources.getLayout(Resources.java:857)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.Spinner.onMeasure(Spinner.java:289)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:599)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1163)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.ScrollView.onMeasure(ScrollView.java:312)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:599)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.View.measure(View.java:12881)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.os.Looper.loop(Looper.java:137)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at android.app.ActivityThread.main(ActivityThread.java:4441)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at java.lang.reflect.Method.invoke(Method.java:511)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-09 17:12:19.621: E/AndroidRuntime(10627):    at dalvik.system.NativeStart.main(Native Method)

stacktrace提到了Spinner,所以错误可能就在那里,但我完全不知道在哪里。所有资源都存在。

任何指南?谢谢。

1 个答案:

答案 0 :(得分:2)

ArrayAdapter adapter=new ArrayAdapter(c, 0, oneto15);

此处传递给适配器的资源ID为0.它应指向有效的XML布局文件,即R.layout.your_layout