Multiple Recyclerview - android

I want to create a chatapp. But in the recyclerview i only can receive the messages from the usermsg databasereference. I dont know how I should code it that i shows 2 recyclerviews. Does someone got an idea? Thanks
Chat Activity
package highelo.drivetogether;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Chat extends AppCompatActivity {
RecyclerView mFriendMsg;
RecyclerView mUserMsg;
private Button SendMsg;
private EditText EditMsg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
final Intent s = getIntent();
final String userID = s.getStringExtra("UID");
final Intent i = getIntent();
final String post_UserID = i.getStringExtra("FID");
SendMsg = (Button) findViewById(R.id.send_msg);
EditMsg = (EditText) findViewById(R.id.edit_msg);
mUserMsg = (RecyclerView) findViewById(R.id.chat_conr);
mUserMsg.setHasFixedSize(true);
mUserMsg.setLayoutManager(new LinearLayoutManager(this));
mFriendMsg = (RecyclerView) findViewById(R.id.chat_conr);
mFriendMsg.setHasFixedSize(true);
mFriendMsg.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
final Intent s = getIntent();
final String userID = s.getStringExtra("UID");
final Intent i = getIntent();
final String post_UserID = i.getStringExtra("FID");
DatabaseReference FriendMsg = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Chats").child(post_UserID).child("Chat");
DatabaseReference UserMsg = FirebaseDatabase.getInstance().getReference().child("Users").child(post_UserID).child("Chats").child(userID).child("Chat");
final FirebaseRecyclerAdapter<Blog, BlogViewHolder2> firebaseRecyclerAdapter2 = new FirebaseRecyclerAdapter<Blog, BlogViewHolder2>(
Blog.class,
R.layout.inchat_row2,
BlogViewHolder2.class,
UserMsg) {
#Override
protected void populateViewHolder(BlogViewHolder2 viewHolder, Blog model, int position) {
viewHolder.setNachricht(model.getNachricht());
viewHolder.setImage(Chat.this, model.getFImage());
}
};
mUserMsg.setAdapter(firebaseRecyclerAdapter2);
final FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.inchat_row,
BlogViewHolder.class,
FriendMsg) {
#Override
public void populateViewHolder(final BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setNachricht(model.getNachricht());
viewHolder.setImage(Chat.this, model.getFImage());
}
};
mFriendMsg.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder2 extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder2(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.finchat_mess);
post_Nachricht.setText(Nachricht);
}
public void setImage(Context ctx, String FImage){
ImageView post_image = (ImageView)mView.findViewById(R.id.finchat_image);
Picasso.with(ctx).load(FImage).fit().centerCrop().into(post_image);
}
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.uinchat_mess);
post_Nachricht.setText(Nachricht);
}
public void setImage(Context ctx, String FImage){
ImageView post_image = (ImageView)mView.findViewById(R.id.uinchat_image);
Picasso.with(ctx).load(FImage).fit().centerCrop().into(post_image);
}
}
}
chat_activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="492dp"
android:id="#+id/chat_conr"
android:layout_weight="1.11"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/send_msg"
android:layout_alignEnd="#+id/send_msg"
android:layout_above="#+id/edit_msg" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_msg"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/send_msg"
android:layout_toLeftOf="#+id/send_msg" />
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Senden"
android:id="#+id/send_msg"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Inchat_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:id="#+id/uinchat_image"
app:civ_border_width="1dp"
app:civ_border_color="#ff0000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/uinchat_mess"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingLeft="5dp"
android:textSize="13dp"
android:paddingBottom="4dp"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/uinchat_image"
android:layout_alignBottom="#+id/uinchat_image" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Inchat2_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:id="#+id/finchat_image"
app:civ_border_width="1dp"
app:civ_border_color="#ff0000"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/finchat_mess"
android:layout_alignParentEnd="false"
android:paddingLeft="5dp"
android:textSize="13dp"
android:paddingBottom="4dp"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/finchat_image"
android:layout_alignBottom="#+id/finchat_image" />
</RelativeLayout>
</android.support.v7.widget.CardView>

The usual way to have a chat between two people is to model that chat room in your database:
ChatRooms
ChatRoomBetweenUser1AndUser2
-KLM.......24
name: "User One"
uid: "uidOfUser1"
text: "Hello there. This is your friend User One"
-KMN.......35
name: "User Two"
uid: "uidOfUser2"
text: "Hello User One, this is User Two. Over"
That way you only need to load data from one location and need only one RecyclerView.
For more information on how to efficiently model chat rooms names for this scenario, see Best way to manage Chat channels in Firebase.
To get a good start on building a chat app, follow the Firebase Codelab for Android.
For a good introduction into modeling your data as your app needs it, see this article on NoSQL data modeling.

Related

Firebase UI Recyclerview OnClick Not Working, tried everything. And it is not a duplicate question please

I'm trying to add a Firebase Recyclerview in my Android App. When I add, all the data is getting fetched from Firestore normally, but when it comes to handle onClick event, it is not working at all.
Things I followed:
Added Interface with method.
Implemented interface in my TipsActivity.java
Here is the code:
TipsActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import android.util.Log;
import com.firebase.ui.firestore.paging.FirestorePagingOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class TipsActivity extends AppCompatActivity implements FirestoreTipsAdapter.OnListItemClick {
FirestoreTipsAdapter firestoreTipsAdapter;
FirebaseFirestore firebaseFirestore;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips);
firebaseFirestore = FirebaseFirestore.getInstance();
recyclerView = findViewById(R.id.list);
Query query = firebaseFirestore.collection("DailyTips").document("MyTips").collection("Tips");
PagedList.Config config = new PagedList.Config.Builder()
.setInitialLoadSizeHint(10)
.setPageSize(5)
.build();
FirestorePagingOptions<TipsModel> firestorePagingOptions = new FirestorePagingOptions.Builder<TipsModel>()
.setLifecycleOwner(this)
.setQuery(query,config,TipsModel.class)
.build();
firestoreTipsAdapter = new FirestoreTipsAdapter(firestorePagingOptions,this,this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(firestoreTipsAdapter);
}
#Override
public void onItemClick() {
Toast.makeText(this, "Show up bruh!", Toast.LENGTH_SHORT).show();
Log.d("AT_LEAST","You should work");
}
}
And here goes my:
FirestoreTipsAdapter.java
package com.mycompany.company;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.paging.FirestorePagingAdapter;
import com.firebase.ui.firestore.paging.FirestorePagingOptions;
public class FirestoreTipsAdapter extends FirestorePagingAdapter<TipsModel, FirestoreTipsAdapter.TipsViewHolder> {
private OnListItemClick onListItemClick;
Context context;
public FirestoreTipsAdapter(#NonNull FirestorePagingOptions<TipsModel> options,OnListItemClick onListItemClick,Context context) {
super(options);
this.onListItemClick = onListItemClick;
this.context = context;
}
#Override
protected void onBindViewHolder(#NonNull TipsViewHolder holder, int position, #NonNull TipsModel model) {
holder.title.setText(model.getTitle());
holder.description.setText(model.getDescription());
}
#NonNull
#Override
public TipsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new TipsViewHolder(view);
}
public class TipsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title,description;
public TipsViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.list_title);
description = itemView.findViewById(R.id.list_desc);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Are you working bro?", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onClick(View v) {
onListItemClick.onItemClick();
}
}
public interface OnListItemClick{
void onItemClick();
}
}
Here is the code of list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
android:id="#+id/tipCardView"
app:cardElevation="5dp"
app:cardBackgroundColor="#E2E0EE"
app:cardCornerRadius="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/list_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:background="?attr/selectableItemBackground"
android:padding="16dp">
<TextView
android:id="#+id/list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="Description" />
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/curveshape"
android:layout_gravity="end|bottom"
android:layout_marginBottom="-30dp"
android:alpha="0.2"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/tips"
android:layout_gravity="end|bottom"
android:layout_marginBottom="-10dp"
android:layout_marginRight="25dp"
android:alpha="0.2"
/>
</androidx.cardview.widget.CardView>
Note: I'm able to fetch data from Firestore, it is showing data properly.
Please help. I followed all other answers from Stack Overflow.
In the given setup, the OnClickListener is being set on the ViewHolder's itemView, which will be the root View in its layout, which is the CardView. However, the clickable and focusable attributes set on the LinearLayout cause it to get first grabs on touch events, so it's basically intercepting them before the CardView would handle them to respond to a click. There's no listener on the LinearLayout, though, so nothing happens.
Assuming that you want the entire item View clickable, simply remove the android:clickable="true" and android:focusable="true" attributes from the <LinearLayout>. With no clickable or focusable children, the CardView will then end up registering the click.
If instead you might want only a certain child clickable – e.g., the LinearLayout – then you would set the OnClickListener on that child, rather than the whole CardView. You still wouldn't need those attributes anywhere, though, if that's to be the only clickable child or grandchild. Those attributes usually aren't necessary in basic, relatively flat layouts, like that for your list items.

I have a crash when add DFP to my app android

I try add DFP Banner Ads to my app , All steps have been applied in https://developers.google.com/ad-manager/mobile-ads-sdk/android/banner .
but if I open my app it crash
crash:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.doubleclick.PublisherAdView.loadAd(com.google.android.gms.ads.doubleclick.PublisherAdRequest)' on a null object reference
at com.englishprofor.all.fragment.FragmentCategory.onCreateView(FragmentCategory.java:61)
FragmentCategory.class
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.englishprofor.all.AppController;
import com.englishprofor.all.Constant;
import com.englishprofor.all.R;
import com.englishprofor.all.activity.MainActivity;
import com.englishprofor.all.activity.SettingActivity;
import com.englishprofor.all.helper.CircleImageView;
import com.englishprofor.all.helper.SettingsPreferences;
import com.englishprofor.all.model.Category;
import com.englishprofor.all.model.SubCategory;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
import com.google.android.gms.ads.doubleclick.*;
import java.util.ArrayList;
public class FragmentCategory extends Fragment {
private static final String TAG = "FragmentCategory";
Context con ;
private RecyclerView recyclerView;
private View view;
ArrayList<Category> mListItem;
//AdView mAdView;
private PublisherAdView mPublisherAdView;
TextView empty_msg, tvTitle;
CoordinatorLayout layout;
ImageView back, setting;
public static ArrayList<Category> categoryList;
public static ArrayList<SubCategory> subCatList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_category, container, false);
mPublisherAdView = view.findViewById(R.id.publisherAdView);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
mPublisherAdView.loadAd(adRequest);
fragment_category.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="#layout/actionbar_layout"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/category_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout"
android:layout_above="#+id/banner_AdView"/>
<TextView
android:id="#+id/txtblanklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#color/black"
android:textSize="15dp"
android:textStyle="bold"
android:visibility="gone" />
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/banner_AdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="/6499/example/banner">
</com.google.android.gms.ads.doubleclick.PublisherAdView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
please help me , I want enable DFP in my app
thanks for all
You got a wrong id of view in below LOC, that why mPublisherAdView be NULL
mPublisherAdView = view.findViewById(R.id.publisherAdView);
should be
mPublisherAdView = view.findViewById(R.id.banner_AdView);

How to add and view child objects in Firebase (Android)?

I am having a similar issue in which I have my files as follow in which I want to be able to view all child items and add new child items.
I understand that you will have to modify the code in the Java file, but I am having trouble understanding how to manipulate my own code and I tried to follow the confusing documentation provided by Firebase.
Below is the structure of my database followed by my code. Any help would be greatly appreciated.
The only problem I have now is how to properly populate the listview with the values of the following structure of my database.
It is as follows: Messages --> Message --> value of which has concatenated string
I hope this isn't confusing
Here is my MainActivity.java
package com.example.sean.messengerappsean;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
import com.firebase.client.collection.ArraySortedMap;
import com.firebase.client.realtime.util.StringListReader;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//TextView mTextFieldCondition;
EditText editUsername, editValue;
ArrayAdapter arrayAdapter;
Button btnSend;
Firebase mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
//mTextFieldCondition = (TextView) findViewById(R.id.textViewCondition);
editUsername = (EditText) findViewById(R.id.editUsername);
editValue = (EditText) findViewById(R.id.editValue);
btnSend = (Button) findViewById(R.id.btnSend);
mRef = new Firebase("https://androidmessagetest.firebaseio.com/"); //CAN USE THE CODE HERE TO PUBLISH TO SPECIFIC LOCATION OR DB
//TRYING TO SET LISTVIEW
final ListView listView = (ListView) findViewById(R.id.listview);
final ArrayList<String> msgsArray = new ArrayList<String>(); //USE THIS FOR POPULATING THE VIEW
final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//----------CONDITION FOR LISTING DATA----------------
//MIGHT NEED TO PUT VALUES INTO ARRAY AND READ THEM OUT IN ORDER OF PUBLISH
//String text = dataSnapshot.child("Message").child("Enter Username3").getValue(String.class);
//mTextFieldCondition.setText(text);
//POPULATE LISTVIEW
for (DataSnapshot Datasnapshot : dataSnapshot.getChildren()) {
msgsArray.add(Datasnapshot.child("Messages").child("Message").getKey());
}
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, msgsArray);
listView.setAdapter(arrayAdapter);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//NEED TO DYNAMICALLY CHANGE VALUE TO USERNAME: MESSAGE
String textoutput = String.valueOf(editUsername.getText());
String MessageText = String.valueOf(editValue.getText());
//FIXED MESSAGE ADD CHILD AND HOW TO ADD VALUE TO MESSAGE
//TRY TO USE BELOW CODE FOR MESSAGES LATER ON
mRef.child("Messages").child("Message").push().setValue(textoutput + ": " + MessageText);
}
});
//ADD FUNCTION TO LOOK FOR DATA
}
}
Here is my XML file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.example.sean.messengerappsean.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--<TextView
android:id="#+id/textViewCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mTextFieldCondition"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp"
android:text="Condition" />-->
//ENTER IN CODE BELOW FOR VIEWING OUTPUT OF DATA
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Send" />
<EditText
android:id="#+id/editValue"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btnSend"
android:layout_toLeftOf="#+id/btnSend"
android:layout_toStartOf="#+id/btnSend"
android:backgroundTint="#color/colorPrimaryDark"
android:inputType="textMultiLine"
android:text="Enter Message Here" />
<EditText
android:id="#+id/editUsername"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnSend"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:background="#color/colorAccent"
android:inputType="textCapWords"
android:text="Enter Username" />
</RelativeLayout>
These imports are for classes in the legacy SDK:
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
These are for the new SDK:
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
You cannot mix the two SDKs. You should use the new SDK only. Remove this statement from your build.gradle dependencies:
compile 'com.firebase:firebase-client-android:2.x.x'
and make the approriate code changes to use the new SDK. Documentation and examples are here. There is also an Upgrade Guide.

Android Activity not Responding

I have 3 activities in my app and when user press next button, next activity is shown to user, Now everything works fine except when it reaches the last activity i.e. when user presses next on 2nd last activity, an error message is shown that app has stopped working and there is no error in LogCat, following is the .java file of my Final Activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class FinalActivity extends ActionBarActivity {
TextView name,address,phone,email,dob,matg,mati,interg,interi,graddeg,gradi,cgpa,skills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
name=(TextView)findViewById(R.id.fname);
address=(TextView)findViewById(R.id.faddress);
phone=(TextView)findViewById(R.id.fphone);
email=(TextView)findViewById(R.id.femail);
dob=(TextView)findViewById(R.id.fdob);
matg=(TextView)findViewById(R.id.matricgrade);
mati=(TextView)findViewById(R.id.matricinst);
interg=(TextView)findViewById(R.id.intergrade);
interi=(TextView)findViewById(R.id.interinst);
graddeg=(TextView)findViewById(R.id.graddegree);
cgpa=(TextView)findViewById(R.id.gradcgpa);
skills=(TextView)findViewById(R.id.iskills);
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills=in.getStringExtra("skills");
name.setText("Name : "+bo.name);
address.setText("Address : "+bo.address);
email.setText("Email : "+bo.email);
phone.setText("Phone : "+bo.phone);
dob.setText("DOB : "+bo.dob);
matg.setText("Matric Grade : "+bo.mg);
mati.setText("Institution : "+bo.mi);
interg.setText("Inter Grade : "+bo.ig);
interi.setText("Institution : "+bo.ii);
graddeg.setText("Graduation Degree : "+bo.gg);
gradi.setText("Institution : "+bo.gi);
skills.setText("Skills : "+bo.skills);
}
}
Here is the xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.first.FinalActivity"
tools:ignore="MergeRootFrame" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/faddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/femail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricgrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/intergrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/interinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/graddegree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradcgpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/iskills"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/hobbies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Here i'm calling the final activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity2 extends ActionBarActivity {
Button next3;
EditText s1,s2,s3,s4,s5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
next3=(Button)findViewById(R.id.next3);
s1=(EditText)findViewById(R.id.sk1);
s2=(EditText)findViewById(R.id.sk2);
s3=(EditText)findViewById(R.id.sk3);
s4=(EditText)findViewById(R.id.sk4);
s5=(EditText)findViewById(R.id.sk5);
next3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills+=s1.getText().toString()+","+s1.getText().toString()+","+s2.getText().toString()+","+s3.getText().toString()+","+s4.getText().toString()+","+s5.getText().toString();
Intent i=new Intent(getApplicationContext(),FinalActivity.class);//this,same
System.out.println(bo.name);
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
i.putExtra("skills", bo.skills);
startActivity(i);
}
});
}
Here is Activity1
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity1 extends ActionBarActivity {
Button next2;
EditText matGrade,matInst,iGrade,iInst,gDegree,gInst,gCgpa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
next2=(Button)findViewById(R.id.next2);
matGrade=(EditText)findViewById(R.id.mgrade);
matInst=(EditText)findViewById(R.id.minst);
iGrade=(EditText)findViewById(R.id.igrade);
iInst=(EditText)findViewById(R.id.iinst);
gDegree=(EditText)findViewById(R.id.gdegree);
gInst=(EditText)findViewById(R.id.ginst);
gCgpa=(EditText)findViewById(R.id.cgpa);
next2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=matGrade.getText().toString();
bo.mi=matGrade.getText().toString();
bo.ig=iGrade.getText().toString();
bo.ii=iGrade.getText().toString();
bo.gg=gDegree.getText().toString();
bo.gi=gDegree.getText().toString();
bo.cgpa=gDegree.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity2.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
startActivity(i);
}
});
}
}
Here is the main Activity
package com.example.first;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button next1;
EditText name,address,email,phone,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next1=(Button)findViewById(R.id.next1);
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.address);
email=(EditText)findViewById(R.id.email);
phone=(EditText)findViewById(R.id.phone);
date=(EditText)findViewById(R.id.date);
next1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UserBO bo=new UserBO();
bo.name=name.getText().toString();
bo.address=address.getText().toString();
bo.email=email.getText().toString();
bo.phone=phone.getText().toString();
bo.dob=date.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity1.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
startActivity(i);
}
});
}
}
Please help me find the mistake
I think you have null pointer exception. Maybe one of your controls is missed in your xml layout. For example you are finding "next1" in your code and using its setOnClickListener method while it is not defined in you xml layout so its object is null.
I recommend you put the content of onCreate method in a try-catch and debug your application.

android layout is not displaying

I know its probably something simple I am missing here, but for some reason I can't seem to get my layout to display when I run my app using the emulator. Any feedback would be great. Thanks
my fragment
package com.pctoolman.planme.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class PlanMeMainFragment extends Fragment {
private Button mNewButton, mExistingButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.planme_main_fragment, container, false);
mNewButton = (Button)v.findViewById(R.id.new_event_button);
mNewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
getActivity().startActivity(myIntent);
}
});
return v;
}
}
my layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ff3a0b">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/plan_me"
android:textSize="36dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center|top"
android:textAlignment="center"
android:textColor="#2c58ff"
android:id="#+id/textView">
</TextView>
</LinearLayout>
<Button
android:id="#+id/new_event_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:padding="5dp"
android:text="#string/new_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
<Button
android:id="#+id/existing_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="#string/existing_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
</RelativeLayout>
here is the activity file
package com.pctoolman.planme.app;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class PlanMeMainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.planme_main_activity);
}
}
You should make a transatcion via FragmentManager to show your fragment in the activity. Suppose you have container (FrameLayout, for example) with id = fragment_container in your planme_main_activity.xml. If so, you should add the following after setContentView(R.layout.planme_main_activity);:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new PlanMeMainFragment())
.commit();
But if you declared fragment directly in XML, please post your planme_main_activity.xml
Maybe it's because you haven't positioned the buttons properly. It's easy to move things around in a relative layout. You don't need to use gravity. You can use:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btn"
android:layout_below="#+id/btn"
I'm assuming you cut out the closing tag for the RL.

Categories

Resources