[已解决] 工作代码:
声明: private View view;
代码: view = inflater.inflate(R.layout.fragment_main, container, false);
替代解决方案:
声明: private View view;
代码:
View v = inflater.inflate(R.layout.fragment_main,container, false);
[All Views inside here];
return v;
或简单:
代码: View view = inflater.inflate(R.layout.fragment_main, container, false);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
我的编码问题一直让我误以为:“无法解析符号视图”。我试图清理构建,使cashe /重启项目无效,但没有任何影响。问题仍然存在。这适用于以下代码中使用的所有“视图”:
quizLinearLayout = (LinearLayout) `view`.findViewById(R.id.quizLinearLayoutEasy);
questionNumberTextViewEasy = (TextView) `view`.findViewById(R.id.questionNumberTextViewEasy);
flagImageView = (ImageView) `view`.findViewById(R.id.flagImageViewEasy);
guessLinearLayouts = new LinearLayout[1];
guessLinearLayouts[0] = (LinearLayout) `view`.findViewById(R.id.row1LinearLayoutEasy);
guessLinearLayouts[1] = (LinearLayout) `view`.findViewById(R.id.row2LinearLayoutEasy);
answerTextView = (TextView) `view`.findViewById(R.id.answerTextViewEasy);
return `view`;
以下是我的java代码,展示了如何使用这些代码:
public class MainActivityFragment extends Fragment {
...
private LinearLayout quizLinearLayout; // layout that contains the quiz
private TextView questionNumberTextViewEasy; // shows current question #
private ImageView flagImageView; // displays a flag
private LinearLayout[] guessLinearLayouts; // rows of answer Buttons
private TextView answerTextView; // displays correct answer
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
fileNameList = new ArrayList<>();
quizCountriesList = new ArrayList<>();
random = new SecureRandom();
handler = new Handler();
// load the shake animation that's used for incorrect answers
shakeAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.incorrect_shake);
shakeAnimation.setRepeatCount(3); // animation repeats 3 times
// get references to GUI components
quizLinearLayout = (LinearLayout) view.findViewById(R.id.quizLinearLayoutEasy);
questionNumberTextViewEasy = (TextView) view.findViewById(R.id.questionNumberTextViewEasy);
flagImageView = (ImageView) view.findViewById(R.id.flagImageViewEasy);
guessLinearLayouts = new LinearLayout[1];
guessLinearLayouts[0] = (LinearLayout) view.findViewById(R.id.row1LinearLayoutEasy);
guessLinearLayouts[1] = (LinearLayout) view.findViewById(R.id.row2LinearLayoutEasy);
answerTextView = (TextView) view.findViewById(R.id.answerTextViewEasy);
// configure listeners for the guess Buttons
for (LinearLayout row : guessLinearLayouts) {
for (int column = 0; column < row.getChildCount(); column++) {
Button button = (Button) row.getChildAt(column);
button.setOnClickListener(guessButtonListener);
}//for-loop int column ends here
}//for-loop LinearLayout row ends here
// set questionNumberTextViewEasy's text
questionNumberTextViewEasy.setText(
getString(R.string.question, 1, FLAGS_IN_QUIZ));
return view; // return the fragment's view for display
}//onCreateView ends here
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/quizLinearLayoutEasy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.android.testingflagquiz.MainActivityFragment"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/questionNumberTextViewEasy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/spacing"
android:text="@string/question"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
<ImageView
android:id="@+id/flagImageViewEasy"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/spacing"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="@string/image_description"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/guessCountryTextViewEasy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:text="@string/guess_country"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4c4c4c" />
<LinearLayout
android:id="@+id/row1LinearLayoutEasy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/row2LinearLayoutEasy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/row3LinearLayoutEasy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/answerButton1Easy"
style="@android:style/Widget.Material.Button.Colored"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:lines="2"
android:text="New Button"
android:textColor="@color/button_text_color" />
<Button
android:id="@+id/answerButton2Easy"
style="@android:style/Widget.Material.Button.Colored"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorAccent"
android:lines="2"
android:text="New Button"
android:textColor="@color/button_text_color" />
</LinearLayout>
<LinearLayout
android:id="@+id/row4LinearLayoutEasy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/answerButton3Easy"
style="@android:style/Widget.Material.Button.Colored"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:lines="2"
android:text="New Button"
android:textColor="@color/button_text_color" />
<Button
android:id="@+id/answerButton4Easy"
style="@android:style/Widget.Material.Button.Colored"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorAccent"
android:lines="2"
android:text="New Button"
android:textColor="@color/button_text_color" />
</LinearLayout>
<TextView
android:id="@+id/answerTextViewEasy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/answer_size"
android:textStyle="bold" />
</LinearLayout>
我的AndroidManifest.xml(如果有用)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.testingflagquiz">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
你应该这样写
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main,container, false);
questionNumberTextViewEasy = (TextView) v.findViewById(R.id.questionNumberTextViewEasy);
return v;
}
将所有内容移至onCreateView
答案 1 :(得分:1)
首先在顶部声明视图,如
private View view;
然后替换此行
return inflater.inflate(R.layout.fragment_main, container, false);
通过
view = inflater.inflate(R.layout.fragment_main, container, false);
最终守则
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
fileNameList = new ArrayList<>();
quizCountriesList = new ArrayList<>();
random = new SecureRandom();
handler = new Handler();
// load the shake animation that's used for incorrect answers
shakeAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.incorrect_shake);
shakeAnimation.setRepeatCount(3); // animation repeats 3 times
// get references to GUI components
quizLinearLayout = (LinearLayout) view.findViewById(R.id.quizLinearLayoutEasy);
questionNumberTextViewEasy = (TextView) view.findViewById(R.id.questionNumberTextViewEasy);
flagImageView = (ImageView) view.findViewById(R.id.flagImageViewEasy);
guessLinearLayouts = new LinearLayout[1];
guessLinearLayouts[0] = (LinearLayout) view.findViewById(R.id.row1LinearLayoutEasy);
guessLinearLayouts[1] = (LinearLayout) view.findViewById(R.id.row2LinearLayoutEasy);
answerTextView = (TextView) view.findViewById(R.id.answerTextViewEasy);
// configure listeners for the guess Buttons
for (LinearLayout row : guessLinearLayouts) {
for (int column = 0; column < row.getChildCount(); column++) {
Button button = (Button) row.getChildAt(column);
button.setOnClickListener(guessButtonListener);
}//for-loop int column ends here
}//for-loop LinearLayout row ends here
// set questionNumberTextViewEasy's text
questionNumberTextViewEasy.setText(
getString(R.string.question, 1, FLAGS_IN_QUIZ));
return view; // return the fragment's view for display
}//onCreateView ends here