I am working on an app using a CardView menu & I am running into some troubles.
The CardView menu presents different tools & among those, there is "location" which uses a third-party library that I used very recently to retrieve the longitude and latitude.
When the user clicks into this card "location", a popup shows up & after pressing the button "update", I set two textviews in that popup to the retrieved longitude and latitude.
However, I seem to run into a NullPointerException when I try:
longitudeTv.setText("Longitude: " + deviceLongitude);
Here are the main parts of my code:
The Menu Class - the launcher activity:
public class Menu extends AppCompatActivity {
GridLayout menuGrid;
SimpleLocation myLocation;
public static double deviceLongitude;
public static double deviceLatitude;
public static Dialog infoPopupDialog;
static TextView messageTv;
public static Dialog locationPopupDialog;
public static TextView longitudeTv;
public static TextView latitudeTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
menuGrid = (GridLayout) findViewById(R.id.menuGrid);
myLocation = new SimpleLocation(this);
infoPopupDialog = new Dialog(this);
messageTv = (TextView) findViewById(R.id.messageTv);
locationPopupDialog = new Dialog(this);
longitudeTv = (TextView) findViewById(R.id.longitudeTv);
latitudeTv = (TextView) findViewById(R.id.latitudeTv);
// if we can't access the location yet
if (!myLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
CardView dataCard = (CardView) findViewById(R.id.dataCard);
CardView locationCard = (CardView) findViewById(R.id.locationCard);
CardView timeCard = (CardView) findViewById(R.id.timeCard);
CardView websiteCard = (CardView) findViewById(R.id.websiteCard);
CardView emailCard = (CardView) findViewById(R.id.emailCard);
CardView infoCard = (CardView) findViewById(R.id.infoCard);
infoCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.showInfoPopup();
}
});
dataCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent dataActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dataActivity);
}
});
locationCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final double longit = roundCoordinates(myLocation.getLongitude());
final double latit = roundCoordinates(myLocation.getLatitude());
deviceLongitude = longit;
deviceLatitude = latit;
MainActivity.showLocationPopup();
}
});
timeCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Menu.this, "Next update in 11 mns", Toast.LENGTH_SHORT).show();
}
});
websiteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://159.203.78.94/rpilog/weatherstation.txt")); //Insert website.
startActivity(openWebsite);
}
});
emailCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Weather Station Data Report");
intent.putExtra(Intent.EXTRA_TEXT, MainActivity.dataFromURL); //Add string variable holding entire data here.
intent.setData(Uri.parse("mailto:RHellstrom#bridgew.edu"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
}
});
}
//function to round to 2 decimal places our GPS coordinates.
public static double roundCoordinates(double coordinate){
String result = String.format("%.2f", coordinate);
double roundedValue = Double.parseDouble(result);
return roundedValue;
}
public void closePopup(View v){ //ONCLICK OF CLOSE ICON
infoPopupDialog.dismiss();
locationPopupDialog.dismiss();
}
public void updateCoordinates(View v){ //ONCLICK BTN "UPDATE"
Menu.latitudeTv.setText("Latitude: " + Menu.deviceLatitude);
Menu.longitudeTv.setText("Longitude: " + Menu.deviceLongitude);
}
}
Menu.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="com.myapps.toualbiamine.weathertracker.Menu"
android:background="#drawable/bg"
android:weightSum="10"
android:orientation="vertical"
android:backgroundTintMode="multiply"
android:backgroundTint="#color/background"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather Tracker"
android:textSize="34sp"
android:textColor="#color/titleColor"
android:layout_centerInParent="true"/>
</RelativeLayout>
<GridLayout
android:id="#+id/menuGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3"
>
<!-- Row 1 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/dataCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/data"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="32dp"
android:text="Data"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/locationCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="29dp"
android:text="Location"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/timeCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/time"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Update Time"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/emailCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/email"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="35dp"
android:text="Email"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/websiteCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/web"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Website"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/infoCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info"
android:layout_gravity="center_horizontal"
android:layout_marginRight="17dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="34dp"
android:text="Info"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
Popup_location.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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
<ImageView
android:id="#+id/closePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_alignParentRight="true"
android:elevation="5dp"
android:layout_marginTop="7dp"
android:layout_marginRight="7dp"
android:onClick="closePopup"/>
<android.support.v7.widget.CardView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
app:cardBackgroundColor="#color/background"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/location_popup"
/>
<TextView
android:id="#+id/longitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Longitude: "
/>
<TextView
android:id="#+id/latitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Latitude: "
/>
<Button
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="UPDATE"
android:textColor="#color/colorWhite"
android:background="#drawable/update_btn_circle"
android:layout_gravity="center_horizontal"
android:onClick="updateCoordinates"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapps.toualbiamine.weathertracker, PID: 24053
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.
onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.
run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller
.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at
com.myapps.weathertracker.Menu.updateCoordinates(Menu.java:149)
So, the problematics textviews are defined in the popup.xml.
Please, help. I tried, I searched. I can't find anything.
Community, you're my last hope.
PSA: I also have 3 other fragment classes, a viewpager and a a class retrieving data from a URL using the Volley library by Google but they don't seem to coincide with the problem here. Let me know if you need them.
If the views are in your dialog you have to call findViewById on the dialog, as in:
longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);
They can't be found in the menu activity and are null.
You can't call this right away though, it has to be after you have set the layout of the dialog. However, I don't see where you actually set the layout of the dialog to popup_location.xml in the code you posted. Presumably that's in the showLocationPopup method.
The longitudeTv is childview of locationPopupDialog,you need set the layout of the dialog (popup_location.xml). then init with longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);
Related
Dialog was designed by the XML layer and named layout inside it contains a collection of spinner and TextView and some buttons are called from main activity class now I want to know which one of the spinner inside the
D1 () function
I want to define the elements within the layer layout , How do I do that?
//layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="260dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="#f71717">
<LinearLayout
android:id="#+id/l_layout"
android:paddingTop="10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:text="وقت التسليم"
android:textSize="20dp"
tools:ignore="HardcodedText,SpUsage" />
<Spinner
android:id="#+id/tex"
android:layout_width="172dp"
android:paddingRight="40dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:drawSelectorOnTop="true"
android:popupBackground="#c853d7"
style="#style/spinner_style"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry,SpUsage"
android:entries="#array/day_"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl"
android:layout_width="250dp"
android:layout_height="129dp"
android:layout_below="#+id/l_layout"
android:background="#f71717"
android:layout_marginTop="0dp"
tools:ignore="ObsoleteLayoutParam">
<TextView
android:id="#+id/text_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/spinner_minutes2"
android:paddingLeft="10dp"
android:text="ساعة"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_alignStart="#+id/button_holder"
android:paddingLeft="10dp"
android:text="دق"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="#+id/text_pam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="ص/م"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry"
android:layout_marginEnd="12dp"
android:layout_alignBaseline="#+id/spinner_minutes"
android:layout_alignBottom="#+id/spinner_minutes"
android:layout_alignEnd="#+id/spinner_minutes3" />
<Spinner
android:id="#+id/spinner_minutes"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/spinner_style"
android:layout_alignStart="#+id/spinner_minutes2"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes2"
android:layout_width="85dip"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_marginStart="16dp"
style="#style/spinner_style"
android:layout_toEndOf="#+id/text_m"
android:entries="#array/fruits" />
<Spinner
android:id="#+id/spinner_minutes3"
android:layout_width="85dip"
android:layout_height="wrap_content"
style="#style/spinner_style"
android:paddingRight="20dp"
android:entries="#array/apm"
tools:ignore="RtlHardcoded,RtlSymmetry"
android:layout_alignBaseline="#+id/spinner_minutes2"
android:layout_alignBottom="#+id/spinner_minutes2"
android:layout_toEndOf="#+id/spinner_minutes" />
<TextView
android:id="#+id/text_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<LinearLayout
android:id="#+id/button_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_minutes"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:layout_marginTop="20dip">
<Button
android:id="#+id/button_set"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:text="Set"
tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />
<Button
android:id="#+id/button_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginRight="10dip"
android:text="Cancel"
tools:ignore="ButtonOrder,ButtonStyle,HardcodedText,RtlHardcoded" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
// class: mainactivity
public class MainActivity extends AppCompatActivity {
Button buttonstartSetDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
d1();
}});
}
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
about_dlg.show();
//Spinner spinner = (Spinner) findViewById(R.id.spinner);
}
}
to access to Spinner in your layout :
public void d1(){
Dialog about_dlg = new Dialog(MainActivity.this);
about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
about_dlg.setContentView(R.layout.layout);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
about_dlg.show();
}
This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 4 years ago.
This is the code MainActivity.java file. I think there should be no mistake in this code. But it shows the message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
I don't know why it is showing up there? Please help me to figure out.
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
import android.view.*;`
public class MainActivity extends AppCompatActivity {
int score=0,wickets=0;
String value=null,number=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button b1=(Button)findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
Button b2=(Button)findViewById(R.id.b_i2);
Button b3=(Button)findViewById(R.id.b_i3);
Button b4=(Button)findViewById(R.id.b_i4);
Button b6=(Button)findViewById(R.id.b_i6);
Button bw=(Button)findViewById(R.id.b_w);
Button bwide=(Button)findViewById(R.id.b_wide);
Button a1=(Button)findViewById(R.id.a_i1);
Button a2=(Button)findViewById(R.id.a_i2);
Button a3=(Button)findViewById(R.id.a_i3);
Button a4=(Button)findViewById(R.id.a_i4);
Button a6=(Button)findViewById(R.id.a_i6);
Button aw=(Button)findViewById(R.id.a_w);
Button awide=(Button)findViewById(R.id.a_wide);
TextView ta= (TextView)findViewById(R.id.a);
TextView tb= (TextView)findViewById(R.id.b);
public void aorb(View view{
if(getResources().getResourceEntryName((view.getId())).startsWith("a"))
{ value="a";}
else{value="b";}
number=getResources().getResourceEntryName((view.getId())).substring(2);
score();}
public void a_enabled(Boolean b)
{
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b)
{
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s){
if(value.equals("a"))
{
ta.setText(s);
b_enabled(false);
a_enabled(true);
}
else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score(){
int i=0;
if(number.equals("i1"))
i=1;
if(number.equals("i2"))
i=2;
if(number.equals("i3"))
i=3;
if(number.equals("i4"))
i=4;
if(number.equals("i6"))
i=6;
if(number.equals("wide"))
i=1;
score+=i;
if(number.equals("w"))
wickets+=1;
if(wickets==10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score+"/"+wickets);
if(value.equals("a"))
{
value="b";
}
else{value="a";}
score=0;
wickets=0;
display(score+"/"+wickets);
}
display(score+"/"+wickets);
}
public void reset(View view){
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}}
This is the code for my content_main.xml file.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/a_i1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/a_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/a_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/a_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/a_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/a_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/a_wide"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team B"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/b"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/b_i1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/b_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/b_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/b_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/b_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/b_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/b_wide"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:onClick="reset"
android:layout_marginBottom="32dp"
/>
</RelativeLayout>
Here is the image of it should look like it is the screenshot the preview
All these findViewById must be included in your onCreate() method. You can keep the declarations out of onCreate(). Your views do not exist before setContentView(R.layout.activity_main);
I mean before onCreate() you can have Button b1;
and after setContentView(R.layout.activity_main); in onCreate() you must have b1=(Button)findViewById(R.id.b_i1);
for every view.
I have made the corrections in your and now the app is not crashing. Try using the below code snippet
MainActivity
public class MainActivity extends AppCompatActivity {
int score = 0, wickets = 0;
String value = null, number = null;
Button b1;
Button b2;
Button b3;
Button b4;
Button b6;
Button bw;
Button bwide;
Button a1;
Button a2;
Button a3;
Button a4;
Button a6;
Button aw;
Button awide;
TextView ta;
TextView tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
b1 = (Button) findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
b2 = (Button) findViewById(R.id.b_i2);
b3 = (Button) findViewById(R.id.b_i3);
b4 = (Button) findViewById(R.id.b_i4);
b6 = (Button) findViewById(R.id.b_i6);
bw = (Button) findViewById(R.id.b_w);
bwide = (Button) findViewById(R.id.b_wide);
a1 = (Button) findViewById(R.id.a_i1);
a2 = (Button) findViewById(R.id.a_i2);
a3 = (Button) findViewById(R.id.a_i3);
a4 = (Button) findViewById(R.id.a_i4);
a6 = (Button) findViewById(R.id.a_i6);
aw = (Button) findViewById(R.id.a_w);
awide = (Button) findViewById(R.id.a_wide);
ta = (TextView) findViewById(R.id.a);
tb = (TextView) findViewById(R.id.b);
}
public void aorb(View view) {
if (getResources().getResourceEntryName((view.getId())).startsWith("a")) {
value = "a";
} else {
value = "b";
}
number = getResources().getResourceEntryName((view.getId())).substring(2);
score();
}
public void a_enabled(Boolean b) {
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b) {
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s) {
if (value.equals("a")) {
ta.setText(s);
b_enabled(false);
a_enabled(true);
} else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score() {
int i = 0;
if (number.equals("i1"))
i = 1;
if (number.equals("i2"))
i = 2;
if (number.equals("i3"))
i = 3;
if (number.equals("i4"))
i = 4;
if (number.equals("i6"))
i = 6;
if (number.equals("wide"))
i = 1;
score += i;
if (number.equals("w"))
wickets += 1;
if (wickets == 10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score + "/" + wickets);
if (value.equals("a")) {
value = "b";
} else {
value = "a";
}
score = 0;
wickets = 0;
display(score + "/" + wickets);
}
display(score + "/" + wickets);
}
public void reset(View view) {
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team A"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/a_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/a_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/a_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/a_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/a_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team B"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/b_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/b_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/b_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/b_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
Hope this helps.
I have an activity which is not parent in that I want to call a fragment in that activity on click of an image view. So I have added onclickListner on my image view. I am getting toast on click of image view but the fragment is not getting called. What can be the issue?? Can anyone help please..
GoSend activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:id="#+id/LinearLayoutGoSend">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/LinearContainer">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/DriversNear"
android:id="#+id/textView10"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp" />
<fragment android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/map"
tools:context=".GoSend"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.00"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/From"
android:id="#+id/textView11"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="08dp"
android:id="#+id/imageView11"
android:background="#drawable/line2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView10"
android:layout_marginStart="20dp"
android:background="#drawable/ic_place_black_48dp"
android:layout_marginTop="05dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_from"
android:text="#string/Loc"
android:visibility="visible"
android:drawableEnd="#drawable/ic_chevron_right_black_24dp"
android:layout_marginEnd="10dp"
android:cursorVisible="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView12"
android:layout_marginStart="20dp"
android:background="#drawable/ic_description_black_48dp"
android:layout_marginTop="05dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_from_details"
android:layout_marginEnd="10dp"
android:hint="Location details"
android:cursorVisible="true"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView12"
android:layout_marginStart="48dp"
android:hint="House no./Floor/Landmark" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginStart="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contactDetailsFrom"
android:layout_marginEnd="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawableEnd="#drawable/ic_expand_more_black_24dp"
android:text="#string/additionalContact"
android:layout_gravity="center_vertical"
android:layout_marginTop="05dp"
android:layout_marginStart="05dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView15"
android:layout_marginStart="48dp"
android:hint="House no./Floor/Landmark" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/LinearLayoutAdditionalContactTo">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView18"
android:layout_marginStart="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contactDetailsTo"
android:layout_marginEnd="10dp"
android:text="#string/additionalContact"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawableEnd="#drawable/ic_expand_more_black_24dp"
android:layout_marginTop="05dp"
android:layout_marginStart="05dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginEnd="50dp"
android:layout_gravity="center"
android:visibility="gone"
android:id="#+id/LinearLayoutTo">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTex"
android:layout_weight="1"
android:hint="Name"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editTe"
android:layout_weight="1"
android:hint="Phone"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OR"
android:id="#+id/textView"
android:layout_gravity="center" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView_next"
android:layout_gravity="center_horizontal"
android:layout_weight="0.42"
android:background="#drawable/ic_chevron_right_black_48dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
GoSend activity
public class GoSend extends AppCompatActivity {
private GoogleMap mMap;
private MarkerOptions markerOptions;
private LinearLayout ll;
private TextView additionalContactFrom;
private TextView additionalContactTo;
private LinearLayout linearLayoutFrom;
private LinearLayout linearLayoutTo;
private ImageView next;
private Toolbar toolbar;
private EditText editTextLocation;
private EditText edtxt_from;
private EditText edtxt_to;
private String stringAddress;
String stringAddress1;
String stringAddressTo;
EditText locdetailsFrom;
EditText locdetailsTo;
private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gosendlayout);
setUI();
if (Build.VERSION.SDK_INT >= 21) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void setUI() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("COURIER");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
edtxt_from=(EditText)findViewById(R.id.editText_from);
edtxt_to=(EditText)findViewById(R.id.editText_to);
locdetailsFrom = (EditText) findViewById(R.id.editText_from_details);
locdetailsTo = (EditText) findViewById(R.id.editText_to_details);
additionalContactFrom = (TextView)findViewById(R.id.contactDetailsFrom);
additionalContactTo = (TextView)findViewById(R.id.contactDetailsTo);
linearLayoutFrom = (LinearLayout)findViewById(R.id.LinearLayoutFrom);
linearLayoutTo = (LinearLayout)findViewById(R.id.LinearLayoutTo);
next = (ImageView)findViewById(R.id.imageView_next);
try {
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
edtxt_from.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),PickLocationActivity.class);
startActivity(i);
}
});
edtxt_to.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),PickLocationActivity.class);
startActivity(i);
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
additionalContactTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutTo.getVisibility() == View.GONE){
linearLayoutTo.setVisibility(View.VISIBLE);
}else{
linearLayoutTo.setVisibility(View.GONE);
}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetailsFragment fragment = new DetailsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.LinearContainer, fragment).commit();
Toast toast = Toast.makeText(getApplicationContext(),"Done",Toast.LENGTH_LONG);
toast.show();
}
});
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
}
}
DetailsFragment layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top"
android:background="#android:color/transparent">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:color/white"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Details"
android:id="#+id/textView22"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/payment"
android:id="#+id/textView24"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView23"
android:background="#drawable/line2"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:weightSum="1">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView24"
android:background="#drawable/coins49"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/price"
android:id="#+id/textView25"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView26"
android:layout_gravity="center_vertical"
android:layout_marginStart="190dp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView26"
android:background="#drawable/line2"
android:layout_marginTop="05dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView25"
android:layout_marginStart="10dp"
android:background="#drawable/currency13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/total"
android:id="#+id/textView27"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView28"
android:layout_gravity="center_vertical"
android:layout_marginStart="190dp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView27"
android:background="#drawable/line2"
android:layout_marginTop="05dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView28"
android:layout_marginStart="10dp"
android:background="#drawable/credit101" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/paywith"
android:id="#+id/textView29"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView30"
android:layout_gravity="center_vertical"
android:layout_marginStart="180dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView29"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/ic_chevron_right_black_48dp" />
</LinearLayout>
DetailsFragment
public class DetailsFragment extends Fragment {
private Toolbar toolbar;
public DetailsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_details, container, false);
return view;
}
}
Please help..
There is nothing in your activity layout with android:id="#+id/LinearContainer, and so FragmentManager does not have anywhere to place your DetailsFragment.
I want to call one fragment onclick of next image view. So I have put OnClickListener on image view but it is not getting called at all..
What can be the issue??
GoSend xml Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:id="#+id/LinearLayoutGoSend">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/DriversNear"
android:id="#+id/textView10"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />
<fragment android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/map"
tools:context=".GoSend"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.00"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/From"
android:id="#+id/textView11"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="08dp"
android:id="#+id/imageView11"
android:background="#drawable/line2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView10"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_place_black_48dp"
android:layout_marginTop="05dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_from"
android:text="#string/Loc"
android:visibility="visible"
android:drawableRight="#drawable/ic_chevron_right_black_24dp"
android:layout_marginRight="10dp"
android:cursorVisible="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView12"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_description_black_48dp"
android:layout_marginTop="05dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_from_details"
android:layout_marginRight="10dp"
android:hint="Location details"
android:cursorVisible="true"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView12"
android:layout_marginLeft="48dp"
android:hint="House no./Floor/Landmark" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayoutAdditionalContactFrom">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView13"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_person_black_48dp"
android:layout_marginTop="05dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/contactDetailsFrom"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawableRight="#drawable/ic_expand_more_black_24dp"
android:text="#string/additionalContact"
android:layout_gravity="center_vertical"
android:layout_marginTop="05dp"
android:layout_marginLeft="05dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView13"
android:layout_marginLeft="48dp"
android:hint="Input if you're not sender" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView15"
android:layout_marginLeft="48dp"
android:hint="House no./Floor/Landmark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView16"
android:layout_marginLeft="48dp"
android:hint="input if you're not receiver" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="1.00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Details"
android:id="#+id/textView17"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="08dp"
android:id="#+id/imageView16"
android:background="#drawable/line2"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView"
android:layout_marginLeft="20dp"
android:background="#drawable/ic_check_circle_black_48dp"
android:layout_marginTop="05dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/pickup"
android:id="#+id/textView18"
android:layout_marginLeft="10dp"
android:layout_marginTop="05dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/imageView20"
android:layout_marginLeft="20dp"
android:background="#drawable/box5"
android:layout_marginTop="05dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText_pac"
android:layout_marginRight="10dp"
android:hint="Items to deliver"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView_next"
android:layout_gravity="center_horizontal"
android:layout_weight="0.42"
android:clickable="true"
android:background="#drawable/ic_chevron_right_black_48dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Calling this in GoSend Activity
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetailsFragment fragment = new DetailsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.LinearLayoutGoSend, fragment).commit();
}
});
GoSend activity
public class GoSend extends AppCompatActivity {
private GoogleMap mMap;
private MarkerOptions markerOptions;
private LinearLayout ll;
private TextView additionalContactFrom;
private TextView additionalContactTo;
private LinearLayout linearLayoutFrom;
private LinearLayout linearLayoutTo;
private ImageView next;
private Toolbar toolbar;
private EditText editTextLocation;
private EditText edtxt_from;
private EditText edtxt_to;
private String stringAddress;
String stringAddress1;
String stringAddressTo;
EditText locdetailsFrom;
EditText locdetailsTo;
private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gosendlayout);
setUI();
if (Build.VERSION.SDK_INT >= 21) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void setUI() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("COURIER");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
edtxt_from=(EditText)findViewById(R.id.editText_from);
edtxt_to=(EditText)findViewById(R.id.editText_to);
locdetailsFrom = (EditText) findViewById(R.id.editText_from_details);
locdetailsTo = (EditText) findViewById(R.id.editText_to_details);
additionalContactFrom = (TextView)findViewById(R.id.contactDetailsFrom);
additionalContactTo = (TextView)findViewById(R.id.contactDetailsTo);
linearLayoutFrom = (LinearLayout)findViewById(R.id.LinearLayoutFrom);
linearLayoutTo = (LinearLayout)findViewById(R.id.LinearLayoutTo);
next = (ImageView)findViewById(R.id.imageView_next);
next.setClickable(true);
try {
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
edtxt_from.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),PickLocationActivity.class);
startActivity(i);
}
});
edtxt_to.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i=new Intent(getApplicationContext(),PickLocationActivity.class);
startActivity(i);
}
});
additionalContactFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutFrom.getVisibility() == View.GONE){
linearLayoutFrom.setVisibility(View.VISIBLE);
}else{
linearLayoutFrom.setVisibility(View.GONE);
}
}
});
additionalContactTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linearLayoutTo.getVisibility() == View.GONE){
linearLayoutTo.setVisibility(View.VISIBLE);
}else{
linearLayoutTo.setVisibility(View.GONE);
}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetailsFragment fragment = new DetailsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.LinearLayoutGoSend, fragment).commit();
}
});
}
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Bundle bundle = getIntent().getExtras();
if(bundle != null)
stringAddress = bundle.getString("address");
}
}
DetailsFragment
public class DetailsFragment extends Fragment {
private Toolbar toolbar;
public DetailsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_details, container, false);
return view;
}
}
Fragment layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top"
android:background="#android:color/transparent">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#android:color/white"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Details"
android:id="#+id/textView22"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/payment"
android:id="#+id/textView24"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView23"
android:background="#drawable/line2"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:weightSum="1">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView24"
android:background="#drawable/coins49"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/price"
android:id="#+id/textView25"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView26"
android:layout_gravity="center_vertical"
android:layout_marginStart="190dp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView26"
android:background="#drawable/line2"
android:layout_marginTop="05dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView25"
android:layout_marginStart="10dp"
android:background="#drawable/currency13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/total"
android:id="#+id/textView27"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView28"
android:layout_gravity="center_vertical"
android:layout_marginStart="190dp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="08dp"
android:id="#+id/imageView27"
android:background="#drawable/line2"
android:layout_marginTop="05dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView28"
android:layout_marginStart="10dp"
android:background="#drawable/credit101" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/paywith"
android:id="#+id/textView29"
android:layout_gravity="center_vertical"
android:layout_marginStart="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView30"
android:layout_gravity="center_vertical"
android:layout_marginStart="180dp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView29"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/ic_chevron_right_black_48dp" />
</LinearLayout>
Please Help...
why are you using these below lines of code, please remove them and check the effect:-
android:clickable="true"
next.setClickable(true);
It's not realize to work but it works in maximum cases.
If you set the view to be clickable, then it will consume the click and not propagate it to the container.
I have added an imageswitcher to my register fragment, when the user select his/her gender the image changes but the image shown is too big and I cant scale it. I am using a scrollview and it is not good that the Imageswitcher is bigger than screen. I tried using smaller images but the result is same. Here is my XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/registerbackground"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/kayit_kayit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/register"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/kayit_ad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:hint="#string/hintAd"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/kayit_soyad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:hint="#string/hintSoyad"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/kayit_email"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:hint="#string/hintEmail"
android:inputType="textEmailAddress"
android:textColor="#FFFFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/kayitKiz"
android:textColor="#FFFFFFFF" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/kayitErkek"
android:textColor="#FFFFFFFF" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageSwitcher
android:id="#+id/cinsiyetSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageSwitcher>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="#+id/kayit_kelime1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:hint="#string/hintKelimeler1"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFFFF"/>
<EditText
android:id="#+id/kayit_kelime2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/hintKelimeler2"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFFFF"/>
<EditText
android:id="#+id/kayit_kelime3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="50dp"
android:hint="#string/hintKelimeler3"
android:inputType="textNoSuggestions"
android:textColor="#FFFFFFFF"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
and the code for the imageswitcher:
....
cinsiyetSwitcher = (ImageSwitcher) rootView.findViewById(R.id.cinsiyetSwitcher);
cinsiyetSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return imageView;
}
});
kizRadio = (RadioButton) rootView.findViewById(R.id.radio0);
erkekRadio = (RadioButton) rootView.findViewById(R.id.radio1);
kizRadio.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
degistir1(v);
}
});
erkekRadio.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
degistir2(v);
}
});
....
...
public void degistir1(View view)
{
Animation in = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.slide_out_right);
cinsiyetSwitcher.setInAnimation(in);
cinsiyetSwitcher.setOutAnimation(out);
cinsiyetSwitcher.setImageResource(R.drawable.kari);
}
public void degistir2(View view)
{
Animation in = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.slide_out_right);
Animation out = AnimationUtils.loadAnimation(getActivity(),
android.R.anim.slide_in_left);
cinsiyetSwitcher.setInAnimation(out);
cinsiyetSwitcher.setOutAnimation(in);
cinsiyetSwitcher.setImageResource(R.drawable.adam);
}