當我嘗試實現下面這段代碼時,它會給出下面的錯誤。我不明白的是EditText
輸入字段的靜態內容如何?這可能是非常基本的,我可能會在這裏失蹤。這是如何編輯文本字段靜態內容?
Error:(28, 32) error: non-static method findViewById(int) cannot be referenced from a static context
代碼:
package com.example.kheriaa.meme_fragments_v1;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class TopSectionFragment extends Fragment {
private EditText TopText;
private EditText BottomText ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frags_layout, container, false);
TopText = (EditText) View.findViewById(R.id.TopText);
//return super.onCreateView(inflater, container, savedInstanceState);
return v;
}
}
這是因爲我認爲目標是 「V」?而不是查看? – supersayan
@supersayan是因爲View的實例是v,所以你應該使用v.findViewById(R.id.TopText); –
@supersayan我補充說明 – rafsanahmad007