为什么我的项目中会弹出ResourceNotFound错误?

时间:2016-11-04 13:46:47

标签: java android xml robolectric

我正在Android工作室创建我的第一个Robolectric测试,但会弹出下一个错误:

  

android.content.res.Resources $ NotFoundException:无法找到   资源ID#0x7f040019

我已经尝试了好几件事。清理,重建和重新加载项目。我检查了我的资源,包括' activity_main.xml'。无论我使用什么活动,错误都发生在下一行:

setContentView(R.layout.activity_main);

完整错误:

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)

这些是我的一些项目文件:

  

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"));
}
}

有谁知道如何修复此错误?

2 个答案:

答案 0 :(得分:0)

将此添加到click方法

之外
EditText editText = (EditText)findViewById(R.id.editText);

根据你的代码findViewById获取本地视图内容(在按钮内)。里面没有EditText。

答案 1 :(得分:0)

将RoboTest.java中的@RunWith(RobolectricTestRunner.class)更改为@RunWith(RobolectricGradleTestRunner.class)帮助了我!