2016-11-04 22 views
2

私はAndroidのスタジオで私の最初のRobolectricテストを作成していますが、次のエラーがポップアップ表示されます:Projectで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

RoboTest.java内の@RunWith(RobolectricTestRunner.class)を@RunWith(RobolectricGradleTestRunner.class)に変更すると、私を助けました。

+0

RobolectricGradleTestRunnerは推奨されていません – fobbymaster

0

は(ボタン内部の)ローカルビューのコンテンツを取得findViewByIdあなたのコードに従ってクリック方法外

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

にこれを追加します。その中にEditTextはありません。

+0

残念ながら、これでエラーは消えません。また、私はそれらのEdittextフィールドをコメントアウトしていません。でも助けてくれてありがとう:) – Klyner

+0

エラーが発生した行は何ですか? @Klyner –

+0

どのアクティビティを使用するかは関係ありません。エラーは次の場所で発生します。setContentView(R.layout.activity_main); @Ishan Fernando – Klyner

関連する問題