2012-02-28 41 views
8

Tôi đang cố gắng chạy một bánh mì nướng theo thứ tự để hiển thị một nguồn cấp dữ liệu rss ruuning. Tôi nhận được lỗi sau khi chạy: java.lang.RuntimeException: Toast này không được tạo với Toast.makeText()lỗi khi hiển thị bánh mì nướng

Mã của tôi là:

LayoutInflater inflater = getLayoutInflater(); 
View layout = inflater.inflate(R.layout.toast_layout, 
           (ViewGroup) findViewById(R.id.toast_layout_root)); 

ImageView image = (ImageView) layout.findViewById(R.id.toastimage); 
image.setImageResource(R.drawable.bball_icon); 
TextView text = (TextView) layout.findViewById(R.id.toasttext); 

Toast toast = new Toast(getApplicationContext()); 
toast.setView(layout); 
for (int i=0;i<episode_titles.size();i++) 
{ 
    toast.setText(episode_titles.get(i).toString()); 
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
    toast.setDuration(Toast.LENGTH_SHORT); 

    toast.show(); 

} 
+0

Bạn có thể đăng logcat từ lỗi của mình không? – caiocpricci2

+0

02-28 11: 11: 10.421: E/AndroidRuntime (9715): java.lang.RuntimeException: Bánh mì nướng này không được tạo bằng Toast.makeText() – user1163234

+0

Thực ra chúng ta cần logcat đề cập đến lỗi thực tế, bạn đã đề cập đến việc chạy : java.lang.RuntimeException! – caiocpricci2

Trả lời

2

Các Toast U có thể xác định như thế này .. .

Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show(); 

Sau đó u có thể viết như thế này ...

 String s=episode_titles.get(i).toString(); 
     Toast.makeText(getApplicationContext(), "UrMessage:"+s, Toast.LENGTH_LONG).show(); 
+0

cần xem tùy chỉnh .. – user1163234

1
Toast.makeText(getApplicationContext(), "your text", Toast.LENGTH_LONG).show(); 

Tác phẩm của nó dành cho tôi.

+0

cần xem tùy chỉnh ... – user1163234

4

Bạn chỉ có thể gọi bánh mì nướng.SetText() nếu trước đó bạn đã tạo bánh mì nướng bằng một trong các phương thức makeText. Xem tài liệu của phương pháp này: http://developer.android.com/reference/android/widget/Toast.html#setView(android.view.View)

Trong ví dụ của bạn, bạn nên cập nhật các văn bản bằng cách sử dụng TextView chứ không phải là Toast

+0

đã thử: "text.setText (tập_titles.get (i) .toString()); " nhưng chỉ có kết quả cuối cùng của episode_titles – user1163234

2

Bạn có thể sử dụng này

for (int i=0;i<episode_titles.size();i++) 
{ 
    Toast.makeText(getApplicationContext(), episode_titles.get(i).toString(), Toast.LENGTH_LONG).show(); 
} 
+0

Tôi muốn sử dụng chế độ xem được tùy chỉnh ... – user1163234

0

Thử này ra:

LayoutInflater inflater = getLayoutInflater(); 
View layout = inflater.inflate(R.layout.toast_layout, 
           (ViewGroup) findViewById(R.id.toast_layout_root)); 

ImageView image = (ImageView) layout.findViewById(R.id.toastimage); 
image.setImageResource(R.drawable.bball_icon); 
TextView text = (TextView) layout.findViewById(R.id.toasttext); 

Toast toast = new Toast(getApplicationContext()); 
toast.setView(layout); 
for (int i=0;i<episode_titles.size();i++) 
{ 
    text.setText(episode_titles.get(i).toString()); 
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
    toast.setDuration(Toast.LENGTH_SHORT); 

    toast.show(); 

} 

Hãy cho tôi biết nếu công trình này :)

+0

Tôi chỉ nhận được kết quả cuối cùng từ mảng chứ không phải toàn bộ ... – user1163234

0
Toast toast = new Toast(getApplicationContext()); 
//your for loop here { 
     toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
     toast.setDuration(Toast.LENGTH_LONG); 
     TextView txt1 = new TextView(this); 
     txt1.setText(R.string.hello); 
     toast.setView(txt1); 
     toast.show(); 
    } 
+0

Tôi không hiểu ... – user1163234

+1

Không thể thêm văn bản trực tiếp, – Sandy09

+0

@Andhrudu Có, đúng vậy. Nhìn vào câu trả lời của tôi. – sandalone

14

Để đặt văn bản thành bánh mì nướng, bạn phải khởi chạy văn bản qua makeText.

Như thế này:

Toast toast = Toast.makeText(this, "message", Toast.LENGTH_SHORT); 
    toast.setText("new message"); 
    toast.setGravity(Gravity.CENTER, 0, 0); 
    //other setters 
    toast.show(); 
1

Thay vì toast.setText(episode_titles.get(i).toString());, sử dụng text.setText();.

Các vấn đề liên quan