2015-09-05 32 views
0

我對android非常陌生。我正在製作一個自定義敬酒,但是在屏幕上出現敬酒之後,應用程序正在崩潰。 這是我mainactivity.java -IllegalargumentException在Android中製作自定義敬酒時的問題

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

    public void dosome(View V) { 
     Log.d("message","a message"); 



     if(V.getId() == R.id.launchmap) { 
      Toast toast = new Toast(this); 
      toast.setGravity(Gravity.BOTTOM, 0, 0); 
      toast.setDuration(Toast.LENGTH_LONG); 

      LayoutInflater inflater = getLayoutInflater(); 
      View appear = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.root),true); 
      toast.setView(appear); 
      toast.show(); 
     } 
    } 

} 

這些都是我的XML文件。

toast_layout.xml -

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/images" /> 

activity_main.xml中 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:id="@+id/root" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/launchmarket" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="dosome" 
     android:text="launchmarket" /> 

    <Button 
     android:id="@+id/launchmap" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="dosome" 
     android:text="launchmap" /> 

</LinearLayout> 

然而,在inflater.inflate,如果我通過了第三個參數是假的,一切正常。爲什麼這樣?

+1

http://stackoverflow.com/questions/12567578/what-does-the-layoutinflater-attachtoroot-parameter-mean可能會提供一些見解 – Breavyn

+0

事情是烤麪包不應該是一些佈局的孩子,它是一個獨立的佈局。 – Bhargav

回答

0

事情是烤麪包不應該是你的看法中的一些佈局的孩子,它是一個獨立的佈局。這是你的烤麪包的佈局(你的情況下toast_layout.xml)。

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/images" /> 

你需要讓說的LinearLayout與佈局來包裝這一點,並給ID toast_layout_root該特定的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:id="@+id/toast_layout_root"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/images" /> 
</LinearLayout> 

那麼做到這一點,同時使你的麪包

查看出現= inflater.inflate(R.layout.toast_layout,(ViewGroup中)findViewById(R.id.toast_layout_root),TRUE);

您目前正在做的是嘗試將toast添加到您的活動中的線性佈局。吐司必須是獨立的。