我知道你可以完全禁用即时运行(新的Android 2.0功能).但是我确实喜欢这个功能,除了在某些情况下: - 当对布局文件进行更改时,它通常不会在访问这些资源时获取导致Nullpointer的更改.
有没有办法绕过即时运行?并强制重新安装?
我知道改变AndroidManifest强制这个但这不方便.
我在YT Advanced Android Espresso上找到了很棒的乐器测试教程.我从那里拿了代码,对我的需求进行了小调整.
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;
...
@Test
public void checkToolbarTitle() {
String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops);
onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile)));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它对我不起作用.测试失败:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar)
Run Code Online (Sandbox Code Playgroud)
这有什么问题?我该如何以其他方式测试?
我已经编写了以下通知代码,我可以看到通知的通知和标题,但文本不可见.我尝试了很多次静态值,但仍然没有得到什么问题.请帮助我,提前谢谢.
private void messagenotification(String msg, String nextid) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("id", nextid ).apply();
Intent intent = new Intent(this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.blue)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.blue))
.setContentTitle("Chat")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Run Code Online (Sandbox Code Playgroud) 我需要在我的android设备的SD卡中读取一个文件,并将该文件的内容写入已存在的SD卡中的另一个文件中.
这是我在SD卡中的任何位置读取文件的代码.
public String readFromFile(String fileName) {
String ret = "";
try {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard, fileName);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
if ( bufferedReader != null ) {
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException …Run Code Online (Sandbox Code Playgroud) public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t = Thread.currentThread();
System.out.println(t);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面代码的输出是 - Thread[main,5,main] ?请解释