没有错误或警告但不幸的是app停止了

时间:2014-11-21 13:03:51

标签: android xml eclipse android-layout

我尝试构建一个使用toast显示图像的应用程序...在eclipse中它没有显示任何错误......但是当执行时,按钮点击后输出弹出的时间只有几分之一,问题来了,显示"不幸的是app停止了#34; ...我的MainActivity.java中的代码如下:

    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();
        }
    }

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

我的xml文件" activity_main.xml"和" custom_lay.xml"分别如下:

    <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>

6 个答案:

答案 0 :(得分:0)

1。)你没有发布你的异常堆栈跟踪

2。)我认为您的问题是您使用Toast t = new Toast()而不是Toast.makeText(Context, String, int).show() int Toast.LENGTH_SHORTToast.LENGTH_LONG创建了祝酒词。

答案 1 :(得分: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();
        }
    }

答案 2 :(得分:0)

我相信你在传递

行的根视图时犯了一些错误
View appear=l.inflate(R.layout.custom_lay,(ViewGroup)findViewById(R.id.boss));

尝试在root视图中传递null,如下所示

View appear=l.inflate(R.layout.custom_lay,null);

答案 3 :(得分:0)

这样做,

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();
}}

答案 4 :(得分:0)

我猜你在这里有两种可能的解决方案。

您有以下代码:

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

可以使用以下任一方法修改:

1)

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

2)

View appear=l.inflate(R.layout.custom_lay, null);

答案 5 :(得分:0)

此代码抛出java.lang.IllegalArgumentException: View not attached to window manager。如果您在行下面更改

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

到:

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

它运作得很好。

说明:当您调用inflater.inflate时,根参数是可选的,并且应该是您尝试充气的布局的父级(例如,你试图在列表视图中为单行扩充视图,你将列表视图作为父视图。

我的猜测是,在您的代码中,R.layout.custom_lay没有任何父级?在这种情况下,父级应该为空。

相关问题