setChecked in CheckBox returns java.lang.NullPointerException - java

I hope that you can help me with this. I have a XML with a checkBox control and I want change its to checked by code.
When Android Studio compiles, the code has an error in "cb.setChecked(true)". FATAL EXCEPTION: main
java.lang.NullPointerException
CheckBox cb = (CheckBox) findViewById(R.id.checkBox);
cb.setChecked(true);
XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff8989"
android:id="#+id/backColor"
android:divider="#drawable/ic_launcher"
android:longClickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:layout_gravity="left|top"
android:checked="false"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="..."
android:id="#+id/txt_tarea"
android:paddingLeft="10dp"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
Thanks.

Please check the id that you provide to findViewById. Is it present in your layout.xml. ALso have to inflated the view using that layout file before calling this. Please verify these points and I think your problem will be solved.

Related

My layout's markup moves off using ScrollView with ImageView and TextView

I am a new user in Android dev. Actually I have one problem that I can't solve for much time.
I have a layout with Scroll View, there are ImageView and TextView in this ScrollView. You can see it in my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity ="center"
android:fillViewport="true"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:padding="10dp"/>
<TextView
android:id="#+id/textInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I take the data from JSON and using this layout for input it in.
There are images that have different forms.
So, please, help me with it. What I must to do with layout markup and what scaleType I must to use.
Thanks in advance.
P.S. Sorry for my English)
It would be helpful if you posted the code that takes the data from a json too.
For picking the right scaletype, this might help.
Hello) I have already solve my problem. I need only to add this things
android:scaleType="fitCenter"
android:adjustViewBounds="true"
I just forgot what adjustViewBounds do)

If internet is off getting error java.lang.IllegalStateException: ScrollView can host only one direct child

I am using NestedScrollView with single layout but they give me error like below when Internet is off, if internet on this is working fine.
Process: com.oktested, PID: 8028
java.lang.IllegalStateException: ScrollView can host only one direct child
at androidx.core.widget.NestedScrollView.addView(NestedScrollView.java:444)
at com.google.android.material.snackbar.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:511)
at com.google.android.material.snackbar.BaseTransientBottomBar$1.handleMessage(BaseTransientBottomBar.java:191)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_black">
<androidx.core.widget.NestedScrollView
android:id="#+id/nsvFaces"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginTop="#dimen/_70sdp"
android:fontFamily="#font/roboto_regular"
android:text="#string/browsebyfaces"
android:textColor="#color/colorAccent"
android:textSize="#dimen/_24sdp"
android:layout_marginRight="#dimen/_10sdp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvFaces"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginRight="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_70sdp"
android:alpha="1"
android:background="#drawable/bg_gradient_home" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="#dimen/_10sdp"
android:src="#drawable/ic_vector_arrow" />
<RelativeLayout
android:id="#+id/sideMenu"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="#dimen/_12sdp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_menu_sky" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I have also search more result but not getting proper solution and i have already try every solution from below link but not working in my case.
ScrollView can host only one direct child with relativelayout
Scrollview can host only one direct child
A scroll view can have only one child Android XML file
java.lang.IllegalStateException: ScrollView can host only one direct child
If I am wrong please suggest me and please help me.
at com.google.android.material.snackbar.BaseTransientBottomBar.showView(BaseTransientBottomBar.java:511)
Your stack trace tells me that you try to add a Snackbar to the ScrollView.
The Snackbar is tries to find a parent view where it can add its view. You probably called the Snackbar.make() function with your ScrollView, so it tries to add the Snackbar there.
If you pass the RelativeLayout as a parent to the make function it will hopefully work.

When passing a message from one activity to the next, that message always appears under anything else I write. How do I make it appear on top?

Temperature is the text that needs to be displayed at the bottom and activity_display_message is the 1337 that needs to be displayed at the top.
Add a TextView and use textView.setText(your_string); on it. I'm assuming you are trying to print the text on the LinearLayout currently.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.DisplayMessageActivity">
<TextView
android:id="#+id/activity_display_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temperature"/>
</LinearLayout>
Code,
TextView textView = (TextView) findViewById(R.id.activity_display_message);
textView.setText(your_string);
you can try this
use two text view to show your value and "Temperature" text. you can use RelativeLayout to easily manage show temperature text at bottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_display_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tempValue"
android:textSize="25sp"
android:gravity="center"
android:text="Value"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Temperature"
android:textSize="25sp"
android:gravity="bottom|center"
android:layout_below="#+id/tempValue"/>
</RelativeLayout>
and inside java write
TextView txtTempValue = (TextView)findViewById(R.id.tempValue);
txtTempValue.setText(your_temp_value);

The "R" Class are not recoginzed

My layouts and views' ids, eclipse can not recognize them and underscore them with red line.
I built all the projects and cleaned them as well, but the problem still exists. Please provide any suggestions.
UPDATE:
The updated problem is, the R class is highlighted with red
JavaCode:
setContentView(R.layout.mplmenu_activity);
mMainMenuListView = (ListView) findViewById(R.id.ListView_Menu);
String [] items = {
getResources().getString(R.string.menuItem_AddNewLocation),
getResources().getString(R.string.menuItem_MySavedLocation),
getResources().getString(R.string.menuItem_GPSReadings),
getResources().getString(R.string.menuItem_OpenCameraSurface)
};
ArrayAdapter<String> adapt =new ArrayAdapter<String>(this,
R.layout.mainMenuItems, items);
mplmenu_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/mplMenuActivityBackGroundColor" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal">
<TextView
android:id="#+id/TextView01"
android:layout_height="wrap_content"
android:text="#string/menu"
android:textSize="#dimen/mainMenuTextSize"
android:layout_marginLeft="18dp"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_width="wrap_content"
android:shadowColor="#android:color/white"
android:textColor="#color/mainMenuTextColor">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center">
<ListView
android:layout_height="wrap_content"
android:id="#+id/ListView_Menu"
android:layout_width="wrap_content"
android:divider="#drawable/dividerline"
android:layout_alignParentTop="true">
</ListView>
</RelativeLayout>
</LinearLayout>
Check that the generated R.java file actually exists. You may have errors in some of your XML resource files that prevent the Android build tool aapt from generating the R.java file. In my experience, that is typically why resource IDs are not found.
To fix this, you will have to fix the XML errors.
If you have something like import your_package_name.R in your Activity class, remove it, and clean the project, then build it again.
Make sure that you are identifying them in your xml with the +id and not just id.
Like this:
android:id="+id/btn1"
not this:
android:id="id/btn1"

Unable to scroll

Please have a look at the following android xml file which is responsible for creating GUI
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/imageTitle"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="#string/imageDate"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="254dp"
android:layout_weight="0.34"
android:src="#drawable/test_image" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="136dp"
android:text="#string/imageDescription" />
</LinearLayout>
</ScrollView>
I am trying to scroll the whole screen, but it is not happening. The uploaded images will make it clear to you. Why it is not scrolling? I am new to android and this is my second app. Please help!
for using scroll view, if u have entire view to be scrollable, use scroll view as parent layout and use code like this.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
here android:fillViewport="true" this is important part which allows u to fill the entire screen for your layout. if any other issue ask .
As android.developer says for fillViewport Defines whether the scrollview should stretch its content to fill the viewport.
Change this 2 like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/imageDescription" />
I think you should use wrap_content in the LinearLayout

Categories