CardView NullPointerException onClickListener - android

I am trying to implement a setOnClickListener method on CardView but I keep getting a null exception whenever I switch orientation of device (works fine if I start by either portrait or landscape mode):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.navajoinc.navajoinc.navfeedbackapp/com.navajoinc.navajoinc.navfeedbackapp.feedBackCardView}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
My Java Class is:
public class feedBackCardView extends FragmentActivity {
CardView neutralCustomer;
CardView happyCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back_card_view);
neutralCustomer = findViewById(R.id.neutralCustomer);
happyCustomer = findViewById(R.id.happyCard);
neutralCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
happyCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
}
public void unsatisfiedCutomerSelectionMethod(View v){
singleChoiceClass unsatisfied_customer_dialog = new singleChoiceClass();
unsatisfied_customer_dialog.show(getFragmentManager(), "unsatisfied_customer_dialog");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.contactus:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:800.525.5097"));
startActivity(callIntent);
break;
case R.id.visitsite:
Intent siteIntent = new Intent(Intent.ACTION_VIEW);
siteIntent.setData(Uri.parse("http://navajoinc.com"));
startActivity(siteIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
Activity XML:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".feedBackCardView"
android:padding="10dp"
android:background="#color/colorWhiteSmoke"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center"
android:text="Please tap a response :"
android:textColor="#color/colorLightGrey" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:clickable="false"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/happyCard"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/happy_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Happy Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="I have had a happy experience"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="#+id/neutralLayout"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/neutralCustomer"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/neutral_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Neutral Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was neutral"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:onClick="unsatisfiedCutomerSelectionMethod"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/angry_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Unsatisfied Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was unpleasant"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I have tried many solutions on Stackoverflow and other websites but none of it has worked ? Could you please help with this issue?

I've tried the code and it's working. Even after rotation. However, I changed the implementation of the onclick methods to simply show a toast rather that launch an intent since you didn't provide the classes. I also ommited all the code related to the menu.
Also, why are you setting the clickable=true property on your card view if you are setting an onClickListener within your code? It's redundant. Also, I don't see any reason to assert clickable=false in your <LinearLayout> tags.
I don't think you are providing enough information to locate the error, or it's related to the classes that haven't been provided. Is the app crashing simply on rotation, or after you try clicking on something? Are you sure the crash is related to rotation, and not an attempt to launch a new activity?

Related

i hide some imageViews inside recycler view, but when i do scroll down all imageViews hide

Hello Team!!
I'm new on android but i have knoweledge in html and javascript. So i have a JSON that have some options(img,or not img) and i create a recycler view to list this JSON and all ok, later i try that hide some imageView of card because the JSON show that this doesn't have picture. It works fine
But when i do scroll down to bottom limit all imageViews hide, i don't know how solve it and i search in web but i can't find some solution for this issue.
Code in adapter
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onBindViewHolder(postViewHolder postViewHolder, int position) {
addPostTimeline addPost = addPostTimelines.get(position);
try {
CircleImageView img=postViewHolder.imgFoto;
JSONObject fileson=addPost.getDatosTimeine().getJSONObject("file");
// postViewHolder.imgTimeLine = #+id/image_timeline
if(!fileson.getBoolean("err")){ // si tiene archivos adjuntos
switch (fileson.getString("type")){
case "Pic":
imageTimeline imgTm=new imageTimeline(fileson.getString("src"), postViewHolder.imgTimeLine);
imgTm.execute();
break;
default:
postViewHolder.imgTimeLine.setVisibility(View.GONE);
break;
}
}else{
postViewHolder.contentFiles.setVisibility(View.GONE);
}
putImgProfile putImg=new putImgProfile(addPost.getDatosTimeine().getString("pDir"), img);
putImg.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
Structure of card with recycler
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="#+id/cvManana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
card_view:cardCornerRadius="4dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout android:background="#drawable/textlines"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:orientation="vertical"
android:id="#+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imgPerfil"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginRight="8dp"
android:scaleType="centerCrop"
android:src="#mipmap/ic_load_img" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_fechaPost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hace 44 minutos sep. 22º 16"
android:textColor="#999" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="12dp"
android:text="SonickSeven"
android:textColor="#24650e"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:id="#+id/contentTextStory">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_postTimeLine"
android:textColor="#666"
android:textSize="15dp"
android:layout_gravity="left"
android:text="historias"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contentFiles">
<!-- image to hide -->
<ImageView
android:id="#+id/image_timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitStart"
card_view:srcCompat="#drawable/campo_manana" />
<!-- end imagen to hide -->
</LinearLayout>
<LinearLayout
android:paddingTop="4dp"
android:paddingBottom="7dp"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="right"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_reply"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_reply_gray"/>
<TextView
android:id="#+id/countReply"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_share"
android:scaleType="centerCrop"
android:layout_gravity="center_vertical"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_share_gray"/>
<TextView
android:id="#+id/countShare"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:layout_gravity="center_vertical"
android:id="#+id/bt_like"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_like_gray"/>
<TextView
android:id="#+id/countLike"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.0"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/bt_dislike"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#mipmap/ic_nolike_gray"/>
<TextView
android:id="#+id/countNoLike"
android:text="(3)"
android:textSize="12dp"
android:layout_gravity="center"
android:textColor="#999"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
i saw that when i go to bottom of scroll and up the switch trigger again, but it doesn't could because the recycler view run all array JSON. Sorry i can understand how work android-java
Try this in onBindViewHolder method:
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onBindViewHolder(postViewHolder postViewHolder, int position) {
addPostTimeline addPost = addPostTimelines.get(position);
try {
CircleImageView img=postViewHolder.imgFoto;
JSONObject fileson=addPost.getDatosTimeine().getJSONObject("file");
if(!fileson.getBoolean("err") && fileson.getString("type").equals("pic")){
//image must be visible and no error
postViewHolder.imgTimeLine.setVisibility(View.VISIBLE);
postViewHolder.contentFiles.setVisibility(View.VISIBLE);
imageTimeline imgTm=new imageTimeline(fileson.getString("src"), postViewHolder.imgTimeLine);
imgTm.execute();
}else if(!fileson.getBoolean("err") && !fileson.getString("type").equals("pic")){
//image shouldn't be visible and no error
postViewHolder.imgTimeLine.setVisibility(View.GONE);
postViewHolder.contentFiles.setVisibility(View.VISIBLE);
}else{
//error
postViewHolder.imgTimeLine.setVisibility(View.GONE);
postViewHolder.contentFiles.setVisibility(View.GONE);
}
putImgProfile putImg=new putImgProfile(addPost.getDatosTimeine().getString("pDir"), img);
putImg.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
You need to reset the visible where you don’t need to set the visible.
postViewHolder.imgTimeLine.setVisibility(View.VISIBLE);// reset
if(!fileson.getBoolean("err")){ // si tiene archivos adjuntos
switch (fileson.getString("type")){
case "Pic":
imageTimeline imgTm=new imageTimeline(fileson.getString("src"),
postViewHolder.imgTimeLine);
imgTm.execute();
break;
default:
postViewHolder.imgTimeLine.setVisibility(View.GONE);
break;
}
}else{
postViewHolder.contentFiles.setVisibility(View.GONE);
}

Layout not showing up when using intent to start new activity

I have a fragment class with 6 hard-coded cardviews. I want the user to be able to click on them and it open up another activity. Each opens to different activity. Currently, when the user clicks on the cardview it opens the new activity, but doesn't show the layout. For example, when the user clicks on the science cardview it just shows a blank page, but doesn't show the layout with the button.
Here is my fragment page code:
public class show_class extends Fragment implements
View.OnClickListener {
private CardView science_cv, math_cv, social_studies_cv, english_cv,
tech_cv, other_cv;
private DatabaseReference myRef ;
//List<show_class> list;
// private RecyclerView recycle;
// Button view;
public show_class() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_show_class, container, false);
//find cardview's by ids
science_cv = (CardView) view.findViewById(R.id.science);
math_cv = (CardView) view.findViewById(R.id.math);
social_studies_cv = (CardView) view.findViewById(R.id.social_studies);
english_cv = (CardView) view.findViewById(R.id.English);
tech_cv = (CardView) view.findViewById(R.id.tech);
other_cv= (CardView) view.findViewById(R.id.other);
//set onclicklistener to cardviews
science_cv.setOnClickListener(this);
social_studies_cv.setOnClickListener(this);
math_cv.setOnClickListener(this);
english_cv.setOnClickListener(this);
tech_cv.setOnClickListener(this);
other_cv.setOnClickListener(this);
return view;
}
public void onClick (View v){
Intent i;
switch (v.getId()) {
case R.id.science:
i = new Intent(getContext(), Science_classes.class);
case R.id.math: i = new Intent(getContext(), Math_classes.class);startActivity(i);break;
case R.id.social_studies: i = new Intent(getContext(), Social_studies_classes.class);startActivity(i);break;
case R.id.English: i = new Intent(getContext(), English_classes.class);startActivity(i);break;
case R.id.tech: i = new Intent(getContext(), Technology_classes.class);startActivity(i);break;
case R.id.other: i = new Intent(getContext(), Other_classes.class);startActivity(i);break;
default:break;
}
}
}
Here is my xml for the fragment page:
<FrameLayout
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">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#a4a4a4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:selectableItemBackground"
android:id="#+id/science"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/ic_launcher_background"
android:padding="10dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Science"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show2"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/math"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Math"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:onClick="show3"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/English"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show4"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/social_studies"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Social Studies"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:onClick="show5"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/tech"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:background="#color/colorSecondary"
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/ic_launcher_background"
android:padding="10dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technology"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:onClick="show6"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/other"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:background="#color/colorSecondary">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="10dp"
android:src="#drawable/ic_launcher_background" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"
android:textColor="#000"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView></FrameLayout>
Here is the code for the science class i am trying to go to(it is the same for all):
public class Science_classes extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_classes);
}
}
Here is the xml, just a simple button:
<LinearLayout 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=".Science_classes">
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
there is a missing startActivity(i); and break; statement in the first case case R.id.science: that is the reason that after clicking science you are ending up with maths case and starting a maths activity. Will have to check if that is the problem of the blank screen. Is you maths activity layout empty?
'in your show_class fragment in onClick() you are not starting the Science_classes activity like.'
'in first case of R.id.science'
startActivity(i);

included bottom layout for all the android activites with onclick events not working

I am new to android development. I want to include the same horizontal Scroll view in all android activities.I have defined the layout and Onclick events in one separate activity and extended that class with other activites.But the onclick events are not working
here is my Base Activity
public class Footer extends AppCompatActivity implements View.OnClickListener {
private ImageView img_school, img_group, img_news, img_schemes, img_jobs, img_gallery, img_goddess, img_services, img_census, img_address, img_abt_us;
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imgs_school:
Intent i = new Intent(Footer.this, SchoolActivity.class);
startActivity(i);
break;
case R.id.imgs_galary:
Intent gi = new Intent(Footer.this, GalleryActivity.class);
startActivity(gi);
break;
case R.id.imgs_events:
Intent ni = new Intent(Footer.this, EventsActivity.class);
startActivity(ni);
break;
case R.id.imgs_abtus:
break;
case R.id.imgs_jobs:
Intent ji = new Intent(Footer.this, JobsActivity.class);
startActivity(ji);
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_footer);
initAll();
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
}
public void initAll() {
img_school = findViewById(R.id.imgs_school);
img_news = findViewById(R.id.imgs_events);
img_jobs = findViewById(R.id.imgs_jobs);
img_gallery = findViewById(R.id.imgs_galary);
img_abt_us = findViewById(R.id.imgs_abtus);
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:paddingLeft="#dimen/_20sdp"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_school"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/school"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="School"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_events"
android:clickable="true"
android:src="#drawable/news"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Events"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:paddingRight="#dimen/_50sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_jobs"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/jobs"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Jobs"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:paddingRight="#dimen/_50sdp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgs_galary"
android:clickable="true"
android:src="#drawable/gallery"
android:layout_gravity="center"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="Gallery"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:paddingRight="#dimen/_50sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_gravity="center"
android:id="#+id/imgs_abtus"
android:clickable="true"
android:src="#drawable/abt_us"
android:layout_width="#dimen/_35sdp"
android:layout_height="#dimen/_35sdp" />
<TextView
android:text="About Us"
android:layout_gravity="center"
android:textSize="#dimen/_13sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
And i am extending this activity with other classes
such as
public class SchoolActivity extends Footer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
}
}
And its activity_school xml
<RelativeLayout 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="com.evoqis.manovaru.SchoolActivity">
<include layout="#layout/activity_footer"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
But the onclick events are not happening.
Thanks in adavance
Here is the problem:
In subclass you call:
super.onCreate(savedInstanceState);
Which calls:-
setContentView(R.layout.activity_footer); //which creates a new views
initAll(); //which finds views by id
img_gallery.setOnClickListener(this); //setting listeners.
and then in subclass you call:
setContentView(R.layout.activity_school); which creates new view, and overrides all the listeners you have set in superclass.
How to fix it
Move
img_gallery.setOnClickListener(this);
img_school.setOnClickListener(this);
img_news.setOnClickListener(this);
img_jobs.setOnClickListener(this);
img_abt_us.setOnClickListener(this);
Inside public void initAll(), and in subclass do this:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school);
initAll();
Also you can remove onCreate() method from superclass.

couldnt save which view has focus?

my app contains "Settings" activity which contains 2 imagebuttons "back" and "about", whenever i press any of these buttons i get:
"couldnt save which view has focus because the focused view android.widget.LinearLayout#4055a9e0 has no id"
the xml code for my_settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll2"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="8dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/settings" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tb1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="#string/sett1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_back"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/back_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_about"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/about_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
dont pay attention to the "scrollview" (i dont think the problem is there), just look at the end of the code for the 2 imagebuttons..
and this the java code for Settings:
public class Settings extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_settings);
//code...
ImageButton goBack=(ImageButton)findViewById(R.id.st_ibt1);
ImageButton about=(ImageButton)findViewById(R.id.st_ibt2);
goBack.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, MainActivity.class);
startActivity(intent);
//finish();
}
});
about.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, About.class);
startActivity(intent);
//finish();
}
});
}
so, how to fix this? i dont understand what this error means.. any suggestions
I think the problem is :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
You enable focus to this LinearLayout, but you didn't provide any id for it.

Button inside cardview does not work

<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="horizontal"
android:padding="10dp"
android:paddingTop="8dp"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view"
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/accent"/>
<TextView
android:id="#+id/m_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF6600"
android:textSize="30dp"
android:layout_toRightOf="#id/view"
android:text="hey th"
android:textStyle="bold" />
<Button
android:id="#+id/m_delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/m_name"
android:layout_weight="0"
android:text="Delete" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I have put a button inside like this and this is shown in recycler view.
The problem I am facing is that the button click only works if I long press on the button else if i click normally then intent is called.
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MetricsDAO dao = new MetricsDAO(context);
dao.deleteMetrics(metrices.get(position).getEventName());
dao.close();
Intent i = new Intent(context, HealthGraph.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// ((Activity) context).finish();
}
});
I am doing this inside on onBindViewHolder()..
What is it that i am doing wrong or missing?

Categories

Resources