2016-02-19 26 views
0

我在Android Studio 2.0中出現錯誤「預期的類型ID錯誤資源」。代碼實際上很簡單。我正在嘗試學習意圖,但在setContentView方法中出現錯誤。你能檢查一下嗎?預期的類型id錯誤資源 - Android

  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" 
        /> 
    

回答

0

的setContentView方法不需要findViewById()方法

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

將其更改爲

setContentView(R.layout.activity_main); 
+0

感謝Nolly的答案 – luckystones