类型ID错误的预期资源 - Android

时间:2016-02-19 23:22:13

标签: android android-layout

我在Android Studio 2.0中遇到错误“类型ID错误的预期资源”。 代码实际上非常简单。我试图学习意图但在setContentView方法中有错误。你能检查一下吗?

  1. //代码从这里开始

    package com.example.oksijen02.sorunmuvar;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    
    
    public class MainActivity extends Activity {
    
    private static final int REQUEST_CODE=10;
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //following line getting error of "expected resource of type id.."
            setContentView(findViewById(R.layout.activity_main));
    
        }
    
        // here button action to define an intent and its extra value
        public void onClick(View view){
            EditText text= (EditText) findViewById(R.id.inputforintent);
            String value = text.getText().toString();
           //Define intent
            Intent intent = new Intent(this,ResultActivity.class);
            //add Edit Text value to send next activity via intent
            intent.putExtra("senttext",value);
    
        }
    
    }
    
  2. //我们的MainActivity xml在这里

     //Edit Text in main activity
    <EditText
        android:id="@+id/inputforintent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="60dip"
        android:text="FirstActivity"
        android:textSize="20sp"
        />
    
    //here we have button to pass other page which its code unwritten here  
    <Button
        android:id="@+id/startIntent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/inputforintent"
        android:layout_below="@+id/inputforintent"
        android:onClick="onClick"
        android:text="Calling an intent"
        />
    

1 个答案:

答案 0 :(得分:0)

setContentView方法不需要findViewById()方法

>>> with open(filename,'rb') as f:
...     buff = f.read() # it reads the whole file into memory
...
>>> buff
b'\xaa\xcd\xff\x0f'
>>> out_hex = ['{:02X}'.format(b) for b in buff]
>>> out_hex
['AA', 'CD', 'FF', '0F']

将其更改为

 setContentView(findViewById(R.layout.activity_main));