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>