RadioButton setChecked NullPointerException - java

I don't get this to work and I don't see why.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference
at eu.twenty1media.snakeplus.fragment.MainFragment.onCreateView(MainFragment.java:48)
String lastlevel = msettings.getLevel();
Log.d("MainFragment.java", "Last level from Database: radio"+lastlevel);
RadioGroup rLevelGroup = (RadioGroup) getActivity().findViewById(R.id.radioLevel);
int resourceId = this.getResources().getIdentifier("radio"+lastlevel, "id", getActivity().getPackageName());
//rLevelGroup.check(R.id.radioextreme); //Nullpointer!
//RadioButton levelbtn = (RadioButton) getActivity().findViewById(R.id.radioextreme); //NULLPOINTER
//levelbtn.setChecked(true);
I tried both methods at the bottom, but none of them seems to work, I always get a NPE. I tried using the resourceId String but as you can see a normal resource id aswell.
<RadioGroup
android:id="#+id/radioLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_marginBottom="16dp"
android:gravity="center">
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radionormal"
android:text="#string/level_normal"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radiohard"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:text="#string/level_hard"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
<RadioButton
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/radioextreme"
android:background="#drawable/radiolevel"
android:button="#android:color/transparent"
android:text="#string/level_extreme"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:gravity="center"
android:textColor="#color/orange_1"
android:textStyle="bold"
/>
</RadioGroup>
Thanks for the help.
EDIT1: With commenting the Lines below i don't get a Nullpointer Exception.

If it is an an activity:
RadioButton levelbtn = (RadioButton) findViewById(R.id.radioextreme);
levelbtn.setChecked(true);
If it is in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.page_two, container, false);
RadioButton levelbtn = (RadioButton) v.findViewById(R.id.radioextreme);
levelbtn.setChecked(true);
return v;
}
You can also set a radio button checked within the XML
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="#+id/radioButton" />

Related

NullPointerException Java android studio setMovementMethod(LinkMovementMethod.getInstance())

I'm still new with coding but I have a project with android studio right now. What I wanted to do was to add hyperlinks in a page. I've followed the steps and tutorials to do this very carefully but I can't seem to find the problem. In my Logcat it says
PID: 17644 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
but I don't understand how it was caused. I have read some similar problems stating the findByViewId might be wrong but I don't think that is the case for my problem but I would love to be wrong. Here is my code. Any help is much appreciated.
public class LearnFragment extends Fragment {
private Button search;
private TextView faq_link_1, faq_link_2, faq_link_3, faq_link_4, faq_link_5;
EditText editSearch;
public LearnFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_account, container, false);
faq_link_1 = (TextView) viewRoot.findViewById(R.id.faq1);
faq_link_2 = (TextView) viewRoot.findViewById(R.id.faq2);
faq_link_3 = (TextView) viewRoot.findViewById(R.id.faq3);
faq_link_4 = (TextView) viewRoot.findViewById(R.id.faq4);
faq_link_5 = (TextView) viewRoot.findViewById(R.id.faq5);
faq_link_1.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_2.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_3.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_4.setMovementMethod(LinkMovementMethod.getInstance());
and this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".boundary.LearnFragment">
<AutoCompleteTextView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="28dp"
android:hint="Search..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/b_search"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b_search"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="14dp"
android:layout_weight="1"
android:text="Search"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/search_bar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/faq1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:text="#string/link_period_pain"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:linksClickable="true"/>
<TextView
android:id="#+id/faq2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="27dp"
android:text="#string/menstrual_cramp_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq1"
android:linksClickable="true" />
<TextView
android:id="#+id/faq3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="#string/late_period_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq2"
android:linksClickable="true" />
<TextView
android:id="#+id/faq4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="#string/cervical_screening_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq3"
android:linksClickable="true" />
<TextView
android:id="#+id/faq5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:text="#string/pap_smear_link"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq4"
android:linksClickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Not looking what you're looking for? Use the search bar to find out more!"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to assign an integer value to an imageView in Android Studio?

I am creating a feedback app where a user has to click one of the 5 imageViews (1-5 rating) based on his/her experience. My primary aim is to extract the integer value of this rating from the imageView click and push it to a SQLite database.
I am trying to use setTag() and getTag() but to no avail. Any help would be much appreciated. Thanks in advance.
activity_main.xml -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="151dp"
android:text="Name"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#+id/imageView1"
app:layout_constraintEnd_toStartOf="#+id/editTextPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginLeft="172dp"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="#+id/textViewName"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="47dp"
android:layout_marginRight="47dp"
android:tag="1"
app:layout_constraintEnd_toStartOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewName"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginBottom="64dp"
android:tag="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="64dp"
android:tag="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toEndOf="#+id/imageView4"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginBottom="63dp"
android:tag="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView1"
tools:srcCompat="#tools:sample/avatars" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="92dp"
android:layout_height="103dp"
android:layout_marginStart="53dp"
android:layout_marginLeft="53dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:tag="3"
app:layout_constraintEnd_toStartOf="#+id/imageView4"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="333dp"
android:layout_marginLeft="333dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="340dp"
android:layout_marginRight="340dp"
android:layout_marginBottom="75dp"
android:text="Were you satisfied with our hygiene standards?"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java -
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar;
ImageView twoStar;
ImageView threeStar;
ImageView fourStar;
ImageView fiveStar;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editTextPersonName);
oneStar = (ImageView) findViewById(R.id.imageView1);
oneStar.setTag(1);
oneStar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer) oneStar.getTag(), Toast.LENGTH_SHORT).show();
}
});
}
}
Try to replace oneStar.getTag() with view.getTag()
If you want to get the tag, and display it as a Toast message, the instead of casting it into an Integer object convert it to String using .toString method.
Second parameter of Toast.makeText is interger, but you can't pass any integer. It must be the resource Id of a string (R.string.your_string). Remove "(Integer)" and it should solve your problem.
I don't know exactly what are your requirements and in case you missed it, there is a build in rating barin Android. You can check the tutorial here
it is less code and less error prone if you just add this to ImageView: android:onClick="imageViewClick", and create a handler in MainActivity like this:
public void imageViewClick(View view) {
String username = name.getText().toString()+"\n";
Toast.makeText(MainActivity.this, (Integer)view.getTag(),Toast.LENGTH_SHORT).show();
}

Handling Radio Group and Button in Fragment

first take a look on a part of my fragment xml file `
<LinearLayout
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rbclick"
android:text="0" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="4" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="rbclick"
android:text="5" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:onClick="rbclick"
android:text="6" />
</RadioGroup>
</LinearLayout>
now take look on my fragment java class
public class MealEntry extends Fragment {
private MealEntryViewModel mViewModel;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private static final String TAG = "MealEntry";
public RadioGroup rg;
RadioButton rb;
public static MealEntry newInstance() {
return new MealEntry();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
return view;
}
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(radiobtnid);
}
previously by this procedure i mean the (rbclick function ) can handle the radio group and radio button in Activity(i repeat Activity). but in Fragment the onClick method is not working. how can i resolve this? I've tried this way for fragment.
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)getActivity().findViewById(radiobtnid);
}
I think this handler is not working. cause it's got crash when i click the radio button.
here's is Log cat.
12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.closeup, PID: 3720
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9
Here is solution to handle radio group in fragment.
1.Remove all line android:onClick="rbclick" from each RadioButton in meal_entry_fragment.xml file, and create an id for each RadioButton.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#aae4e4e4"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/group_no_1"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="1" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="4" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="5" />
<RadioButton
android:id="#+id/radio6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="6" />
</RadioGroup>
</LinearLayout>
2.Create an instance of OnCheckedChangeListener in MealFragment class.
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0:
// Write your code here
break;
case R.id.radio1:
// Write your code here
break;
case R.id.radio2:
// Write your code here
break;
case R.id.radio3:
// Write your code here
break;
case R.id.radio4:
// Write your code here
break;
case R.id.radio5:
// Write your code here
break;
case R.id.radio6:
// Write your code here
break;
default:
break;
}
}
};
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
rg = view.findViewById(R.id.group_no_1);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
return view;
}
Argument View v in public void rbclick(View v) is the RadioButton you are looking for
Try using this code below:
public void rbclick (View v){
int radiobtnid=rg.getCheckedRadioButtonId();
rb =(RadioButton)v.findViewById(radiobtnid);
}

ViewGroup is not resolved in Android Studio

I'm creating a small animation app using Android studio. When a button press images should work according to the particular animation.
There's an error with line viewGroup = (viewGroup)findViewById(R.id.ViewGroup);
It shows "Cannot resolve symbol ViewGroup". I typed Alt+Enter and selected a solution. But nothing happened.
public class MainActivity extends Activity {
private ViewGroup viewGroup;
private ImageView imageView, imageView2, imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9,imageView10,imageView11,imageView12;
private Button button,button1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView5= (ImageView) findViewById(R.id.imageView5);
imageView6=(ImageView) findViewById(R.id.imageView6);
imageView7=(ImageView) findViewById(R.id.imageView7);
imageView8=(ImageView) findViewById(R.id.imageView8);
imageView9=(ImageView) findViewById(R.id.imageView9);
imageView10=(ImageView) findViewById(R.id.imageView10);
imageView11=(ImageView) findViewById(R.id.imageView11);
imageView12=(ImageView) findViewById(R.id.imageView12);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TransitionManager.beginDelayedTransition(viewGroup, new Fade());
fade(imageView, imageView2, imageView3,imageView4,imageView5,imageView6);
}
});
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Slide());
toggle(imageView7,imageView8,imageView9,imageView10,imageView11,imageView12);
}
});}
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
private static void fade(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
Here's my activity_main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/myAniLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignBottom="#+id/button"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#mipmap/ic_launcher"
android:layout_alignTop="#+id/imageView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/imageView4"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#android:drawable/star_big_on" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView8"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView9"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView7"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView9"
android:layout_alignStart="#+id/button1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView11"
android:src="#android:drawable/star_big_on"
android:layout_below="#+id/imageView8"
android:layout_alignEnd="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView12"
android:src="#android:drawable/star_big_on"
android:layout_above="#+id/button"
android:layout_alignStart="#+id/imageView4" />
Your logcat shows
"Cannot resolve symbol 'ViewGroup'"
viewGroup=(viewGroup)findViewById(R.id.ViewGroup); // (ViewGroup)
viewGroup Not declare in your XML
(viewGroup) must be (ViewGroup) . (viewGroup)
The ViewGroup class is a subclass of the View class. ViewGroup
instances work as containers for View instances to group View
instances together.
http://developer.android.com/intl/es/reference/android/view/ViewGroup.html
I don't see an element ViewGroup in the xml. You need to add it in the same xml or include it.
As per your xml there is not an any id with ViewGroup in your xml code. please give id to your layout with ViewGroup and cast it as below,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
there was also a spelling mistake in your line,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
As they pointed out, you have no R.id.ViewGroup in your layout.
If you really need the view group try this:
viewGroup = (ViewGroup)findViewById(android.R.id.content)
The problem is that you have,
viewGroup=(viewGroup)findViewById(R.id.ViewGroup);
and you have to change it for this,
viewGroup=(ViewGroup)findViewById(R.id.ViewGroup);
The V must be a capital letter.
Also watch for the id of your Viewgroup in your xml, check if it is R.id.Viewgroup or R.id.viewgroup

Add Dynamic View to RalativeLayout

I'm trying to make a calander app following this tut:
How can I create a weekly calendar view for an Android Honeycomb application?
But i need to add a view to the corresponding RelativeLayout but every time i try to run
the app it chrashes.
My fragment .java file:
public class Calendar extends Activity {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class FragmentCalendar extends Fragment {
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
}
My fragment_Calendar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.TJ.rijschool_app.Calendar"
android:orientation="vertical"
android:padding="0dp"
>
<LinearLayout
android:id="#+id/dayLabelsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" >
<RelativeLayout
android:id="#+id/currentMonthRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1" >
<TextView
android:id="#+id/currentMonthTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text=""
android:textSize="15dp"
android:textStyle="bold"
android:textColor="#color/medium_dark_gray" />
<TextView
android:id="#+id/currentYearTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text=""
android:textSize="13dp"
android:textStyle="bold"
android:textColor="#color/medium_dark_gray" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/sundayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/sundayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/sundayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/sun"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="#+id/dayMarkerView"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#color/strong_blue" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/mondayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/mondayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/mondayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:gravity="center"
android:text="#string/mon"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tuesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/tuesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tuesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/tue"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/wednesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/wednesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/wednesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/wed"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/thursdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/thursdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/thursdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/thu"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/fridayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/fridayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/fridayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/fri"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/saturdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/saturdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/saturdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/sat"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Had to delete the part about time because file was getting to big to post here.
As you can see the View called dayMarkerView has to be added dynamicly on the day it is.
Had to change:
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
TO:
public FragmentCalendar() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
v = inflater.inflate(R.layout.fragment_calendar, container, false);
View pl = v.findViewById(R.id.thursdayHeaderRelativeLayout);
View Paper = new View(pl.getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Paper.setLayoutParams(params);
Paper.setBackgroundColor(getActivity().getColor(R.color.strong_blue));
((RelativeLayout)pl).addView(Paper);
//Zet de datum op zondag
//View tv = v.findViewById(R.id.sundayDateTextView);
//((TextView)tv).setText("13");
return v;
}

Categories

Resources