我正在尝试解决一个问题,但找不到任何特定的解决方案。
基类
open class FeedBase() : Parcelable{
var type : String =""
constructor(parcel: Parcel) : this() {
type=parcel.readString()
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(type)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR: Parcelable.Creator<FeedBase> {
override fun createFromParcel(parcel: Parcel): FeedBase {
return FeedBase(parcel)
}
override fun newArray(size: Int): Array<FeedBase?> {
return arrayOfNulls(size)
}
}}
Run Code Online (Sandbox Code Playgroud)
项目类持有数据类并扩展基类
@Parcelize
data class FeedItem(var feed:Feed) : FeedBase()
Run Code Online (Sandbox Code Playgroud)
& 数据类
@Parcelize
data class Feed(var test:String,var score:Int) : Parcelable
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试跨活动发送数据,并且正在创建我的对象,如下所示
val …Run Code Online (Sandbox Code Playgroud) 我正在编写 Instrumentation 测试,但无法单击我的视图。这是我的看法
<ConstraintLayout......>
<ImageView
android:id="@+id/go_to_next"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/rounded_bg"
android:rotation="180"
android:scaleType="centerInside"
android:src="@drawable/ic_back"
app:layout_constraintBottom_toBottomOf="@+id/mobile_number_edittext"
app:layout_constraintEnd_toEndOf="parent" />
</ConstraintLayout/>
Run Code Online (Sandbox Code Playgroud)
我的constraintLayout 左右两边都有30dp 的边距。我的测试用例如下 -
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.test", appContext.packageName)
onView(withId(R.id.go_to_next)).check(matches(isDisplayingAtLeast(90)))
onView(withId(R.id.go_to_next)).perform(click())
}
Run Code Online (Sandbox Code Playgroud)
它在 click() 上失败并收到错误消息 Error performing 'single click - At Coordinates: 1457, 1213 and precision: 16, 16' on view 'with id: com.test:id/go_to_next'.
如果我为 ImageView 提供 5dp 的边距,这会起作用,但如果没有边距,它就不起作用。我该如何解决?我的视图在布局中完全可见,只是它与最右边对齐。仅供参考,所有动画都已禁用
所以基本上我正在使用空间并尝试添加从数据库版本1到2的迁移,但我的alter命令不起作用我的当前实现如下:
void init() {
db = Room.databaseBuilder(Global.getInstance(),
AppDatabase.class, "feed").addMigrations(MIGRATION_1_2).build();
}
Run Code Online (Sandbox Code Playgroud)
迁移属性:
static final Migration MIGRATION_1_2 = new Migration(1,2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE 'post' ADD COLUMN 'age' INTEGER NOT NULL DEFAULT 0");
Log.d("VROM","Migration");
}
};
Run Code Online (Sandbox Code Playgroud)
数据库实施:
@Database(entities = {Feed.class, DownloadModel.class}, version = 1) public abstract class AppDatabase extends RoomDatabase {
public abstract DaoAccess getFeedDao();}
Run Code Online (Sandbox Code Playgroud)
因此,在将版本从1递增到2之后,execSQL()会执行,但我的数据库中未添加新列.我从app目录中取出了我的数据库并多次检查但是列不在那里.除此之外,如果我杀了我的应用程序再次启动它再次migrate调用该方法,不知道这是否是预期的功能,但它打破了我的功能.我认为迁移只会被调用一次onUpgrade()
我正在使用hashsets,不知怎的,我遇到了NPE.我知道hashset允许空值.所以我试着知道究竟发生了什么,我在java中创建了以下代码(仅用于学习目的)
Java代码 -
public class numbers {
static HashSet<String> s = new HashSet<>();
public static void main(String[] args) {
s.add("a");
s.add(null);
s.add("b");
for(String l:s)
{
System.out.println(l);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它没有给我任何例外.输出将打印到控制台.然后我试着在android中运行相同的代码
Android代码 -
public class MainActivity extends Activity {
HashSet<String> s = new HashSet<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s.add("a");
s.add(null);
s.add("b");
for(String l:s)
{
//System.out.println(l);
Log.i("ExceptionCheck", l);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我运行应用程序.在Log语句中引发空指针异常,我试图读取输出.
任何人都可以解释我的差异吗?