This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm trying to make a marker on Maps with data lat and long from web service, but it returns error null pointer. I do make log data and the lat and long data are retrieved on log successfully. Please help. Thank you in advance.
This is my KDetail.java
private PData data;
private static final String TAG = "KDActivity";
private Call<APIBaseResponse> call;
private RestClient.GitApiInterface service;
private Toolbar toolbar;
private SessionManager sessions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_k_d);
toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
sessions = new SessionManager(this);
Intent i = getIntent();
data = (PData) i.getSerializableExtra("PDItem");
txtIdP = (TextView) findViewById(R.id.txtIdP);
txtCdt = (EditText) findViewById(R.id.txtCreatedDtm);
txtIdP.setText(data.getId_P());
txtCdt.setText(data.getCreatedDtm());
final MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: ");
double lata = Double.parseDouble(data.getLatP());
double lana = Double.parseDouble(data.getLangP());
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lata, lana))
.title("Start") );
}
});
}
This is the error :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.kd.kddupe.Activity.KDetail.onCreate(KDetail.java:115)
Line 115 is :
mapFragment.getMapAsync(new OnMapReadyCallback() {
This is my acitivity_k_d.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
android:background="#color/feed_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg_parent_rounded_corner"
android:elevation="6pt"
android:layout_marginBottom="20dp"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/id_p"
android:id="#+id/txtIdP"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/line1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<fragment android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/line2" />
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="20dp" >
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/created"
android:textSize="15sp"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_weight="4"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtCreatedDtm"
android:background="#drawable/bg_parent_rounded_corner"
android:hint="#string/yyyy_mm_dd"
android:inputType="textMultiLine"
android:minHeight="35dp"
android:textSize="15sp"
android:singleLine="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Check is may be getSupportFragmentManager() or check the id is it map for sure?? Try it instead of getFragmentManager
OK here the problem at your XML file you using the support version of the map fragment and the code you search for the normal one. so to fix it you have two solutions
choose one from the two
1- change the name of the fragment at xml to
android:name="com.google.android.gms.maps.MapFragment"
2- change the MapFragment to SupportMapFragment at your java code
Related
I need help on how to remove the empty spaces between cardViews inside a Grid Layout. The empty space forms below each row of cards.I want to remove this extra space.How do i fix this.I have included a screenshot to clarify this.
Here is my code for my layout XML
<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_below="#+id/toolbar"
android:layout_marginTop="50dp"
android:paddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
app:cardUseCompatPadding="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:paddingTop="2dp"
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="11sp"
android:textAlignment="center" />
<ImageView
android:transitionName="#+id/profile"
android:layout_marginTop="10dp"
android:layout_below="#+id/text_view_name"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/image_view_upload"
android:adjustViewBounds="true"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
My MainActivity layout
<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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
android:background="#000000"
android:clipToPadding="false"/>
</RelativeLayout>
My java MainActivity class
public class MainActivity extends AppCompatActivity implements ImageAdapter.OnItemClickListener {
private RecyclerView recyclerview;
private ImageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new ImageAdapter(this, uploads);
recyclerview.setLayoutManager(new GridLayoutManager(this,3));
recyclerview.setPadding ( 1,1,1,1 );
}
}
Here is a Screenshot
Remove
android:layout_marginTop="50dp"
from your CardView
<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_below="#+id/toolbar"
android:paddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
app:cardUseCompatPadding="true"
android:orientation="vertical">
This question already has answers here:
Dynamically changing the linearlayout width or height on Android
(4 answers)
Closed 4 years ago.
In my application I want set dynamically weight to LinearLayout and for this I write below codes, but when running application show me Force close error!
Force close message in logCat :
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
My XML codes :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#color/white"
tools:context=".activity.UpdateActivity">
<!--Header-->
<RelativeLayout
android:id="#+id/dialogEndCount_headerLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size120"
android:background="#color/tabHomeColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/size40"
android:text="#string/updateDialogTitle"
android:textColor="#color/white"
android:textSize="#dimen/font16" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/dialogUpdate_animate"
android:layout_width="#dimen/size150"
android:layout_height="#dimen/size150"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/size20"
app:lottie_autoPlay="true"
app:lottie_colorFilter="#color/white"
app:lottie_fileName="update.json"
app:lottie_loop="true" />
</RelativeLayout>
<!--Content-->
<RelativeLayout
android:id="#+id/dialogEndCount_contentLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogEndCount_buttonsLay"
android:layout_below="#+id/dialogEndCount_headerLay"
android:layout_marginBottom="#dimen/size10"
android:layout_marginTop="#dimen/size10"
android:background="#drawable/bg_dialog_vip_white">
<!--Type-->
<TextView
android:id="#+id/dialogUpdate_infoTypeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/size15"
android:layout_marginRight="#dimen/size15"
android:layout_marginTop="#dimen/size10"
android:gravity="center_vertical"
android:text="نوع بروز رسانی : اجباری"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<!--Content-->
<ScrollView
android:id="#+id/dialogUpdate_infoContentScrollLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogUpdate_downloadProgress"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_below="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size20">
<RelativeLayout
android:id="#+id/dialogUpdate_infoContentLay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/dialogUpdate_infoContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/updateContent"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<TextView
android:id="#+id/dialogUpdate_infoContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/dialogUpdate_infoContentTitle"
android:layout_marginTop="#dimen/size5"
android:gravity="center_vertical"
android:textColor="#color/favColorON"
android:textSize="#dimen/font14" />
</RelativeLayout>
</ScrollView>
<!--Download layout-->
<com.tellfa.colony.view.DownloadProgressView
android:id="#+id/dialogUpdate_downloadProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size10" />
</RelativeLayout>
<!--Buttons-->
<LinearLayout
android:id="#+id/dialogEndCount_buttonsLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size60"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!--Cancel-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_cancelBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_cancelBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/ic_close"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_cancelBtnImg"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text="#string/updateCancel"
android:textColor="#color/white"
android:textSize="#dimen/font14" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--Direct-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_directBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/green"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_directBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/appro_ic_download_circle"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_directBtnImg"
android:ellipsize="end"
android:gravity="center"
android:layout_marginRight="#dimen/size1"
android:singleLine="true"
android:text="#string/updateOK"
android:textColor="#color/white"
android:textSize="#dimen/font12" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
My JAVA codes:
public class UpdateActivity extends BaseActivity {
#BindView(R.id.dialogEndCount_buttonsLay)
LinearLayout dialogEndCount_buttonsLay;
private Window window;
private Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//Initialize
ButterKnife.bind(this);
activity = this;
window = activity.getWindow();
//Set color to statusBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.tabHomeColor));
}
//Set dynamically weight
dialogEndCount_buttonsLay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
How can I fix this problem?
If you want set dynamically weightSum just use this code :
dialogEndCount_buttonsLay.setWeightSum(3f);
i hope help you
You're mixing LinearLayout and RelativeLayout. Change your layout to use a LinearLayout then weight is an available property on your LayoutParams.
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
params.weight = 1f
So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.
Here is my swapFragment() method that calls my fragment:
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flgenre, genresFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Here is my GenresFragment.java:
public class GenresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private DatabaseReference mPostReference;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
private ArrayList<String> genreList = new ArrayList<>();
private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public GenresFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static GenresFragment newInstance(int columnCount) {
GenresFragment fragment = new GenresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);
if (rootView == null){
Log.e("TEST1", "Is Null");
}else {
Log.e("TEST2", "Not Null");
}
return rootView;
}
public void failure(){
Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(String item);
}
}
And my fragment_mini_add.xml:
<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"
android:background="#80000000"
tools:context="com.nocrat.fanti.ProfileActivity"
android:id="#+id/flgenre">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="#string/add_new_project"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/rLayout1"
android:layout_below="#id/textView4">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/project_name"
android:gravity="center_vertical"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:hint="#string/group_name"
android:inputType="textPersonName"
android:layout_below="#id/editText"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_below="#id/rLayout1">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create"
android:textColor="#FFD700"
android:textSize="18sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:textColor="#FFD700"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here is my onClickListener that calls swapFragment():
addGenre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
swapFragment();
}
});
Here is my 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"
android:fitsSystemWindows="false"
android:background="#ffffff"
android:orientation="vertical"
android:id="#+id/flgenre">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:elevation="40dp"
android:background="#ffffff">
<include
layout="#layout/app_bar_profile"
android:layout_height="190dp"
android:layout_width="match_parent"
android:id="#+id/app1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8musicalnotes64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Genres"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rounded_genres"
android:gravity="center"
android:clickable="true"
android:id="#+id/addGenres">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_add_white_48px" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8resume64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Bio" />
<EditText
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Tell us about yourself..."
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="edit"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8trophy64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Average Rank"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8meeting64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Collaborations"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.
Try to modify you swapFragment() with below code by replacing replace() with add()
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flgenre, genresFragment);
fragmentTransaction.commit();
}
Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.
Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.
Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)
Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.
You should remove android:id="#+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml.
replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml.
then, add this code to your activity xml to contain the fragment
<FrameLayout
android:id="#+id/flgenre"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So when I launch my app it just crashes because the button in my first fragment class cannot be found for some odd reason.
When I removed the
case R.id.button_accept : the program ran fine, so I was thinking it's most likely something I did with the XML in the register_user_fragment. So is there a specific way I need to call the button because there are multiple layouts in one class?
Here's the error.
Caused by: java.lang.IllegalStateException: Required view 'button_accept' with ID 2131296297 for field 'button_accept' was not found. If this view is optional add '#Nullable' (fields) or '#Optional' (methods) annotation.
Here's my baseactivity
public class BaseActivity extends AppCompatActivity implements View.OnClickListener {
//Fragments
Fragment usernameFragment;
Fragment passwordFragment;
//
FragmentManager fragmentManager;
#BindView(R.id.button_login)
Button button_login;
#BindView(R.id.button_accept) //CRASHES HERE.
Button button_accept;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//check if user is logged in or not
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
usernameFragment = new UsernameFragment();
passwordFragment = new PasswordFragment();
fragmentManager = getFragmentManager();
button_login.setOnClickListener(this);
button_accept.setOnClickListener(this);
//setcontentview register page or whatever use is logged into
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_login :
Toast.makeText(this, "testds", Toast.LENGTH_SHORT).show();
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.content_login, usernameFragment)
.commit();
break;
case R.id.button_accept :
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.register_user_fragment, passwordFragment)
.commit();
break;
}
}
XML containing the "button_accept"
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</LinearLayout>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Because the "button_accept" is in your fragment layout , not in your activity layout :).
Your binding a UI component that doesn't exist on your activity layout.
You just made your layout mixed. just remove the Button accept_button from frame layout .
The problem is here you have button in a layout parent in which you are placing fragment dynamically. so remove the button to out side or to upper parent and out side the fragment container.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/ghostWhiteColor">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/ghostWhiteColor"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/register_user_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="100dp">
<TextView
android:id="#+id/register_user_label_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_medium"
android:gravity="center"
android:text="Choose a username"
android:textColor="#color/myBlack"
android:textSize="25sp" />
<TextView
android:id="#+id/register_user_label_below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:paddingTop="35dp"
android:text="Your nickname can be changed."
android:textColor="#color/myBlack"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/register_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/hint_name"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/label_tos"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="#string/label_tos"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
</LinearLayout>
</FrameLayout>
<Button
android:id="#+id/button_accept"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#drawable/oval"
android:text="#string/btn_sign_up"
android:textColor="#android:color/white" />
</android.support.design.widget.CoordinatorLayout>
I'm trying to make a Character sheet for a DnD like game. The point is to take user input and store it for future viewing and changing. Maybe I'm looking for a solution that doesn't exists but I want the EditTexts to stay the way they are if the user closes the app or just goes back to the title screen. Following is part of the xml file for the user input, the java file is basically blank
**<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/scroll_s">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:inputType="textPersonName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/player"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/player"
android:inputType="textPersonName"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/pts_total"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/unspent_pts"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/pts_total"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/unspent_pts"
android:layout_weight="1"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ht"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/wt"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/age"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/height"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/weightt"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/age"
android:inputType="number"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/size_mod"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/appearance"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appearance"
android:inputType="text"/>
</LinearLayout>
</ScrollView>**
Java file
package com.example.mapuchii.gurps;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Character_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Read values from the "savedInstanceState"-object and put them in your textview
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// Save the values you need from your textview into "outState"-object
super.onSaveInstanceState(outState);
}
}
Here is one example:
public class Character_activity extends AppCompatActivity {
private EditText playerEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
playerEditText = (EditText) findViewById(R.id.player);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
playerEditText.setText(savedInstanceState.getString("SavedPlayer", "");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("SavedPlayer", playerEditText.getText().toString());
super.onSaveInstanceState(outState);
}
}
This will save your text over an orientation change. If you want to save it when your process is killed then you need to look at SharedPreferences instead of a Bundle.
http://developer.android.com/training/basics/data-storage/shared-preferences.html