2016-11-04 3 views
2

Я создаю мой первый тест Robolectric в Android студии, но следующая ошибка выскакивает:Почему ошибка ResourceNotFound появляется в моем проекте?

android.content.res.Resources$NotFoundException: Unable to find resource 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")); 
} 
} 

Кто-нибудь есть идеи, как можно исправить эту ошибку?

+0

Чтобы дать вам отличный ответ, это может помочь нам, если у вас есть взгляд на [спросить], если вы еще этого не сделали. Это может быть полезно, если вы можете предоставить [mcve]. – Mat

+0

Спасибо, я постараюсь улучшить вопрос, читая эти страницы! – Klyner

ответ

0

Смена @RunWith (RobolectricTestRunner.class) внутри RoboTest.java в @RunWith (RobolectricGradleTestRunner.class) помогла мне!

+0

RobolectricGradleTestRunner устарел – fobbymaster

0

добавить это вне метода щелчка

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

согласно коду findViewById получить местное содержание вида (внутри кнопки). В этом нет EditText.

+0

К сожалению, это не позволяет ошибке исчезнуть. Кроме того, когда я не комментирую эти поля Edittext. Спасибо за помощь, хотя :) – Klyner

+0

что такое строка ошибка? @Klyner –

+0

Не имеет значения, какую активность я использую, ошибка возникает при: setContentView (R.layout.activity_main); @Ishan Fernando – Klyner

Смежные вопросы