Reputation: 125
[SOLVED] The working code:
Declare: private View view;
Code: view = inflater.inflate(R.layout.fragment_main, container, false);
ALTERNATIVE SOLUTION:
Declare: private View view;
Code:
View v = inflater.inflate(R.layout.fragment_main,container, false);
[All Views inside here];
return v;
OR SIMPLY:
Code: View view = inflater.inflate(R.layout.fragment_main, container, false);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
I have problem with the coding which keeps throwing me the error message: "Cannot resolve symbole view". I've tried to clean the build, invalidate cashe/restart the project, but without any effect. The problem still persists. This goes for all the "views" used in the below codes:
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`;
.
Here's my java code showing how these are used: .
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
.
my XML file
<?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>
.
My AndroidManifest.xml (if of any use)
<?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>
Upvotes: 1
Views: 167
Reputation: 17535
First Declare view on top like
private View view;
then Replace this line
return inflater.inflate(R.layout.fragment_main, container, false);
By
view = inflater.inflate(R.layout.fragment_main, container, false);
Final Code
@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
Upvotes: 1
Reputation: 12803
You should write like this
@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;
}
Move everything inside the onCreateView
Upvotes: 1