Klyner Klyner - 1 month ago 17
Java Question

Why does the ResourceNotFound error pops up in my project?

I am creating my first Robolectric test in Android studio but the next error pops up:


android.content.res.Resources$NotFoundException: Unable to find
resource ID #0x7f040019


I have tried several things already. Cleaning, rebuilding and reloading the project. I checked my resources including 'activity_main.xml'. The error occurs at the next line, no matter what activity I use:

setContentView(R.layout.activity_main);


Full error:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f040019
at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343)
at org.robolectric.shadows.ShadowResources.resolveResName(ShadowResources.java:338)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:429)
at android.content.res.Resources.loadXmlResourceParser(Resources.java)
at android.content.res.Resources.getLayout(Resources.java:1049)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.maarten.smart_parking_android_app.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5933)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at com.example.maarten.smart_parking_android_app.RoboTest.testRoboTest(RoboTest.java:22)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


These are some of my project files:


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parking"
android:id="@+id/parkingTableButton" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Payment"
android:id="@+id/paymentTableButton" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLotTable"
android:id="@+id/pLotTableButton" />

<Button
android:layout_width="59dp"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/addButton" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/removeButton" />
</LinearLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="493dp"
android:id="@+id/testListView"
android:layout_gravity="center_horizontal" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />





Mainactivity.java


public class MainActivity extends Activity {

private ArrayList<DatabaseShowModel> databaseShowModels;
private ListViewAdapter adapter;
private int currentTable = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeContent();
}

private void initializeContent()
{
databaseShowModels = new ArrayList<>();

final ListView testListView = (ListView)findViewById(R.id.testListView);

Button parkingTableButton = (Button)findViewById(R.id.parkingTableButton);
parkingTableButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
updateTableInformation(1);
currentTable = 1;
}
}
);
Button paymentButton = (Button)findViewById(R.id.paymentTableButton);
paymentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateTableInformation(2);
currentTable = 2;
}
});

Button pLotTableButton = (Button)findViewById(R.id.pLotTableButton);
pLotTableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateTableInformation(3);
}
});

Button addButton = (Button)findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateTableInformation(currentTable);
}
});

Button removeButton = (Button)findViewById(R.id.removeButton);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.editText);
editText.setText("TEST");
updateTableInformation(currentTable);
}
});

adapter = new ListViewAdapter(this,getLayoutInflater(),databaseShowModels);

testListView.setAdapter(adapter);

getAllParkings();
}

public void getAllParkings()
{
HttpRequestTask task = new HttpRequestTask(this);
task.execute();
}

public void processJsonFinished(String results)
{
//Process results
int i = 0;
}

private class HttpRequestTask extends AsyncTask<String, Void, String>
{
private MainActivity listener;

public HttpRequestTask(MainActivity listener)
{
this.listener = listener;
}

protected void onPostExecute(String results) {
listener.processJsonFinished(results);
}

@Override
protected String doInBackground(String... params) {
URL url;
String response = "";
HttpURLConnection urlConnection = null;
try {
url = new URL(Constant.API_PARKING_URL);

urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(2000);
urlConnection.setRequestMethod("GET");

if(urlConnection.getResponseCode() == 200)
{
InputStream in = urlConnection.getInputStream();
response = getStringFromInputStream(in);
}
else
{
InputStream in = urlConnection.getInputStream();
response = getStringFromInputStream(in);
}

} catch (Exception e) {
e.printStackTrace();
}

return response;
}
}

private static String getStringFromInputStream(InputStream is) {

BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {

br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();
}

private void updateTableInformation(int id)
{
System.out.println("PRESSED");
//adapter.notifyDataSetChanged();
}
}



RoboTest.java


@Config(constants = BuildConfig.class, sdk = 21,
packageName = "com.example.*user*.smart_parking_android_app")
@RunWith(RobolectricTestRunner.class)
public class RoboTest {

@Test
public void testRoboTest()
{
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
Button b = (Button) activity.findViewById(R.id.removeButton);

b.performClick();

EditText editText = (EditText)activity.findViewById(R.id.editText);

assertThat(editText.getText().toString(), equalTo("TEST"));
}
}


Does anyone have an idea how this error can be fixed?

Answer

Changing @RunWith(RobolectricTestRunner.class) inside RoboTest.java into @RunWith(RobolectricGradleTestRunner.class) helped me!