-7

I tried to build an app that will show an image using toast...in eclipse it showed no errors...but when executed, after the button click the output popped up for only a fraction of second and problem came and displayed that "unfortunately app stopped"...the code in my MainActivity.java is as below:

    package com.example.newt;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity
    {
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void doThis(View v)
        {
            Toast t=new Toast(this);
            t.setGravity(Gravity.CENTER,0,0);
            LayoutInflater l=getLayoutInflater();
            View appear=l.inflate(R.layout.custom_lay,(ViewGroup)findViewById(R.id.boss));
            t.setView(appear);
            t.show();
        }
    }

--------------------------------------------------------------------------------------------------

my xml files "activity_main.xml" and "custom_lay.xml" are as follows respectively:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/boss"
        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.newt.MainActivity" >

        <Button
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click here"
            android:onClick="doThis" />

   </LinearLayout>

---------------------------------------------------------------------------------------------

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:id="@+id/boss">"

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="41dp"
            android:layout_weight="0.02"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.22"
            android:src="@drawable/ic_launcherto" />

    </LinearLayout>
Sharath
  • 691
  • 8
  • 23

6 Answers6

0

1.) you did not post your exception stack trace

2.) I think your problem is that you created the toast with Toast t = new Toast() instead of Toast.makeText(Context, String, int).show() where int is Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • @nobalG fair point, but doesn't he need `getApplicationContext()` for that to work? According to http://developer.android.com/guide/topics/ui/notifiers/toasts.html example code. – EpicPandaForce Nov 21 '14 at 13:08
  • Although the problem could also be at `View appear=l.inflate(R.layout.custom_lay,(ViewGroup)findViewById(R.id.boss));. I guess we DO need a logcat stacktrace. – EpicPandaForce Nov 21 '14 at 13:09
0
public class MainActivity extends Activity
    {
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void doThis(View v)
        {
                Toast t=new Toast(getApplicationContext());
                t.setGravity(Gravity.CENTER,0,0);
                View appear = getLayoutInflater().inflate(R.layout.custom_lay, null);
                t.setView(appear);
                t.show();
        }
    }
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

i believe you have made some mistake in passing root view at line

View appear=l.inflate(R.layout.custom_lay,(ViewGroup)findViewById(R.id.boss));

try passing null in root view like this

View appear=l.inflate(R.layout.custom_lay,null);
iMDroid
  • 2,108
  • 1
  • 16
  • 29
0

do like this,

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Context context = getApplicationContext();

    LayoutInflater inflater = getLayoutInflater();

    View view = inflater.inflate(R.layout.toast, null);

    Toast toast = new Toast(context);
    toast.setView(view);
    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();
}}
0

My guess is you have two possible solutions here.

You have this code:

View appear=l.inflate(R.layout.custom_lay,(ViewGroup)findViewById(R.id.boss));

Which can be modified with either:

1)

View appear=l.inflate(R.layout.custom_lay,(LinearLayout)findViewById(R.id.boss));

2)

View appear=l.inflate(R.layout.custom_lay, null);
Ricardo Sousa
  • 41
  • 1
  • 5
0

This code throws an java.lang.IllegalArgumentException: View not attached to window manager. If you change below line

LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.custom_lay,(ViewGroup) findViewById(R.id.boss));

to :

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.custom_lay, null);

It works just fine.

Description: When you make a call to inflater.inflate, the root parameter is optional, and is supposed to be a parent of the layout you're trying to inflate (for example when you're trying to inflate the view for a single row in a list view, you give the list view as the parent).

My guess is that in your code, R.layout.custom_lay does not have any parent ? In that case the parent is supposed to be null.

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65