My app crashes after starting activity with scrollview - android

I have a problem with an Android application. In the app there is an xml file which contains a RelativeLayout and inside a Scroll view. Inside the scrollview there some images which I want to display. The problem is that when there are 3 ImageView inside the scrollview the activity and the xml page works fine but ,when I add the 4th ImageView , application crashes when I start the activity.
I am posting the code below, the problem occurs when the 4th ImageView is added. Any ideas?
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:alwaysDrawnWithCache="true"
android:fadeScrollbars="true"
android:verticalScrollbarPosition="right">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/Double_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/trouble" />
<ImageView
android:id="#+id/Secret_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/Double_image"
android:scaleType="centerCrop"
android:src="#drawable/secret" />
<ImageView
android:id="#+id/kidnap_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/Secret_image"
android:scaleType="centerCrop"
android:src="#drawable/kidnap" />
<ImageView
android:id="#+id/kidnap2_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/kidnap_image"
android:scaleType="centerCrop"
android:src="#drawable/kidnap2" />
</RelativeLayout>
</ScrollView>
NOTE: The activity which is connected with this xml file doesn't have any functionality.
Here is the Activity class connected with the above xml.
package com.example.thomas.firstcue;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class RoomsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rooms);
}
}
I start the activity from Main through a button click and the function StartActivity
rooms_but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(intentrooms);
}
});
The application starts normally on phone but when I am pressing the button to Start the activity , the app crashes.
On phone error

One possibility of crashing app is your 4th image which reside in drawable have v24 like kidnap.png(v24)
so first delete this image from drawable folder(..\app\src\main\res\drawable-v24) and again paste it in drawable(..\app\src\main\res\drawable) folder with only kidnap.png

Related

Google Maps in middle area of an Activity

I am working with Android studio 2.1.2 . I have checked around and most of the questions either use older versions of Android studio and some old classes that don't apply to my situation.
From file > new Project > I used option Google Maps Activity. I have not changed any of the default code that are in it. Its XML has a Fragment. Now is there a way to bring in this activity as it is into another Activity by means of a Fragment? Basically I am building an app that shows a map in the middle portion of the screen. This middle portion I hope would be a Fragment. The bottom and top area has some components for the user to interact.
I am not trying to make the map come up in a new Activity but rather remain in the middle portion of a particualar Activity.
Or do I just add new UI components to the default Google Maps Activity.
At first I had an idea of creating a Fragment(with its own .java file, XML Layout)then placing the map code in there and taking it over to the Activity where I want it. What would be the best way to get this done? I appreciate any help.
I have realised that either way one can arrive at the same result, a map inside an Activity, alongside other UI Components. Whether Google Maps Activity is used or if an Empty Activity is created and map code implemented in it. My code is included below. I chose an Empty Activity (Android 2.1.2) and then brought in the map code into the .java file and placed the map fragment with tag in the XML Layout of the activity.
The error was that I had used
android:name="com.google.android.gms.maps.MapFragment"
in the XML Layout but declared and initialised mapFragment(the object instance) with SupportMapFragment. The mix up came while copying and pasting code from the Google Documentation. As I was trying to update my post here I spotted the error. The correct thing to do is if you are using MapFragment(API level 12 and over) use it all through. If you decide to use SupportMapFragment(below API level 12) use SupportMapFragment all through.
The code below is the correction. Just in case anyone has the same issue.
XML Layout (activity_main.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/teachers_link"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/parent_link"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="340dp"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:layout_marginTop="12dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search_term" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:id="#+id/select_level"
android:entries="#array/select_level"></Spinner>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/select_course"
android:entries="#array/select_course"></Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_search" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
.java file (MainActivity.java)
package name;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
}

Android Screen Slide action

I am just starting to learn android java in Android Studio.
And I have bum into a question related to screen slide action.
I wanted to create a screen slide action that allow me to have additional options beside my basic layout's option, after the chose of the additional option I will return to my basic layout.
The perfect example that could represent my idea would be the Google Calculator, when user need advance Math symbol, it has a green layout that show up while user sliding the right's edge to the left, and after user choose one Math Symbol, it will return to its basic layout.
This is the screen shot of the calculator,photo belong to the internet
I am not very good at explaining, I hope you guys understand what I am trying to approach.
I have it working using SlidingPaneLayout. Let me know if this works.
XML
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SlidingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello SlidingPaneLayout!" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:background="#F44336"
android:elevation="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello SlidingPaneLayout!" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
Activity
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SlidingPaneLayout slidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.SlidingPanel);
slidingPaneLayout.setSliderFadeColor(ContextCompat.getColor(this, android.R.color.transparent));
slidingPaneLayout.openPane();
}
}

Android: switching from one layout activity to another

I am trying to display a splash screen for 3 seconds and then go to the main screen, both of them being in the same class. However, my problem is that, when I try calling the new layout.activity, before the original layout.acitity for the class Im in, the program crash. Why?
Here is a little example of what I am talkin about:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);//new activity
displaySplash();
setContentView(R.layout.activity_visualizer);//original activity
The only way that I can get this to run is if I comment out the splash activity completely, but I need it! I wont work if I comment out the other layout activity...
Here are my two activities if it helps:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ListView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ImageView
android:id="#+id/splashscreen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/logo"
android:layout_gravity="center"/>
I'd be tempted to have both as fragments and switch between them. However if you don't want to do that you could just have both the views in the same layout and change the visibility after three seconds. Your view would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
tools:context="comp380.musicvisualizer.Visualizer" >
<ImageView
android:id="#+id/splashscreen"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/logo"
android:layout_gravity="center"/>
<ListView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
</ListView>
And then in the Activity do something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout); //the layout above
displaySplash(); // I guess this waits three seconds
// get refs to the views
ImageView splashScreen = (ImageView) findViewById(R.id.splashscreen);
ListView songList = (ListView) findViewById(R.id.song_list);
// swap the visibility
splashScreen.setVisibility(View.GONE);
songList.setVisibility(View.VISIBLE);
}
You cannot call setContentView() twice for an Activity.
One possible solution would be to have the layout of the splash screen in the same place where your original activity is and manage its visibility.
Otherwise, you could have a splash screen activity with R.layout.activity_splash as layout and after 3 sec launch your main activity.
Why not use the same layout for both of them and put them into FrameLayout, after 3 seconds hide the splash layout and show the normal layout?
You can also create two fragments, one SplashFragment and the other NomralFragment and just swap them after 3 secs if you really want to do this in the same class.
I just figured out my problem and you guys will never guess it: my code was failing because the picture I was using for my splash screen was way too big. I randomly changed the pic and everything works now...HUGE SIGH
Thank you all for trying though!

Dummy ImageView not showing image

It is showing the image in the Android Studio preview but doesn't show anything when I'm running this on my device. Any ideas why this happens? What's wrong with my code?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage" />
</RelativeLayout>
Does your class extend Activity? Try extending AppCompatActivity. I had the same issue and this solution fixed it.
In addition to my comment i've added a few possible soultions. It is very strange the image isn't showing already. Does it show in the imageview in the xml gui design view?
Anyway here are two possible solutions:
xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/myimage" />
Also your imageview hasn't been assigned an Id which seems weird something like this would be needed for my second idea:
<ImageView
android:id="#+id/Image_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Java side:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //what ever the name of the activity you are using
Image= (ImageView)findViewById(Image_ID);
Image.setImageResource(Image_ID);
}
Lets hope this helps!
EDIT
<ImageView
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/myimage" />
Note fitXY may lower the quality of the image. try messing with the other scale types

Android: Combining layouts using FrameLayout - Layout Not Visible

I have multiple activities in my app and i want a slidingdrawer to appear in all the activities. I am following the approach given here.
The app starts with a black screen with the slidingdrawer handle.
Issue is that the sliding drawer is visible but the activity layout put in FarmeLayout is not visible.
The activity is there for sure. Toasts came up when the first activity loaded. So I modified my first activity to have only a button on it to go to next activity.
Next I used the direction keys on the AVD, and pressed ok which brought up the the next activity's Toast. Second activity is as well hidden.
So what am I missing???
My Sliding Drawer layout xml (LinearLayout) with farme layout at the end:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<SlidingDrawer
android:id="#+id/slidingDrawer"
android:handle="#+id/drawerHandle"
android:content="#+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:persistentDrawingCache="all">
<ToggleButton
android:id="#+id/drawerHandle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="5dp"
android:textSize="14dp"
android:textStyle="italic"
android:background="#BB00FF"
android:rotation="270"
android:textOn=""
android:textOff=""/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contentLayout"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/todo">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="#string/list_view_" />
</LinearLayout>
</SlidingDrawer>
<FrameLayout
android:id="#+id/act_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#808080"
android:visibility="visible">
</FrameLayout>
</LinearLayout>
DrawerActivity java code:
public abstract class DrawerActivity extends Activity
{
protected LinearLayout fullLayout;
protected FrameLayout actContent;
#Override
public void setContentView(final int layoutResID)
{
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); // Your base layout here
actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
super.setContentView(fullLayout);
}
}
In all my activitie files I have put setContentView(R.layout.XXXXXXXX); in the onCreate method.
So how do I make the activities visible?? What am I doing wrong??
I tried putting in the following but to no effect
actContent.setVisibility(layoutResID);
or
actContent.setVisibility(View.VISIBLE);

Categories

Resources