我最近開始編程Android,並且遇到問題。 我試圖讓字寫在一個EditText中,作爲一個字符串。findViewById(int)方法未定義爲R.layout類型
package com.example.generatoredifrasi;
import android.os.Bundle;
import android.R.layout;
import android.os.Bundle;
public class EditText
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
EditText Text = (EditText) findViewById(R.id.EditText);
String string = Text.getText().toString(); // get the value from the EditText
}
}
findViewById給了我這個錯誤:The method findViewById (int) is undefined for the type R.layout
爲什麼呢?我如何解決這個問題?
有一種更簡單的方法可以將EditText中的單詞寫成字符串嗎?
發佈更多代碼。 – Blackbelt 2014-10-20 15:29:19
我們需要查看代碼的上下文。把你在哪裏(片段,活動等),你是誰得到該佈局變種。 – jonyjm 2014-10-20 15:35:37
除了這個問題的主要範圍之外,你不應該在標準的Java主要方法中放置任何代碼。主要方法在Android本身並不存在 - 作爲初學者,您應該在Activity的onCreate方法或Fragment的onCreateView方法中放置這樣的代碼。 – naweinberger 2014-10-20 15:42:18