我正在尝试显示自定义Toast,但是从我的自动化测试而不是应用程序本身这样做。
布局通胀不起作用。甚至可以对视图和测试项目进行膨胀并显示这些视图吗?
标准吐司是什么工作:
final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity
Toast.makeText(targetActivity, "Hello from Instrumentation", Toast.LENGTH_SHORT).show();
什么不起作用如下:
final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity
LayoutInflater inflater = targetActivity.getLayoutInflater();
View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout
text.setText("Hello from Instrumentation"); // here I get the NullPointerException
Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
解决方案
在不参考LayoutInflater
targetActivity
final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity
// *** !!! ***
LayoutInflater inflater =
(LayoutInflater) getInstrumentation().getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
// getContext() , NOT getTargetContext()
View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout
text.setText("Hello from Instrumentation"); // here I get the NullPointerException
Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
答案 0 :(得分:1)
您好使用以下代码:
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.R;
public class DoToast extends Toast {
/** The Constant TOAST_DURATION. */
private static final int TOAST_DURATION=4000;
/** The layout. */
View layout;
/**
* Instantiates a new do toast.
*
* @param context the context
* @param text the text
*/
public DoToast(Context context, CharSequence text) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.toast, null);
TextView textView = (TextView) layout.findViewById(R.id.text);
textView.setText(text);
DoToast.this.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
DoToast.this.setDuration(TOAST_DURATION);
DoToast.this.setView(layout);
DoToast.this.show();
}
}
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/toast_bg">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="#000000" />
</LinearLayout>
和样式文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#ffffff"
android:startColor="#F2F2F2"
android:type="linear" />
<stroke
android:width="3dp"
android:color="#000000" />
</shape>
如果您需要显示吐司,请使用以下行:
new DoToast(this,"Testing");
如果您有任何疑问,请告诉我。
答案 1 :(得分:1)
我认为问题与上下文有关,你试图从你的测试应用程序R文件中显示一个toast,但是通过一个活动使用应用程序上下文,你想要做的是从仪器获取测试上下文然后使用以下内容创建布局inflater。
Context context = instrumentation.getContext();
LayoutInflater li = LayoutInflater.from(context);