I am studying for the course Android, in one of the tasks there was a problem, what, I do not understand. Most likely something is wrong with text files in resources.
package com.example.opimand.manul5; import android.content.Context; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.webkit.WebView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class DetailActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); WebView webView = (WebView) findViewById(R.id.webView); Intent intent = getIntent(); //получаем строку и формируем имя ресурса String resName = "n" + intent.getIntExtra("head", 0); Log.i("name", resName); Context context = getBaseContext(); //получаем контекст //читаем текстовый файл из ресурсов по имени String text = readRawTextFile(context, getResources().getIdentifier(resName, "raw", "ru.alexanderklimov.manual")); webView.loadDataWithBaseURL(null, text, "text/html", "en_US", null); } //читаем текст из raw-ресурсов public static String readRawTextFile(Context context, int resId){ InputStream inputStream = context.getResources().openRawResource(resId); InputStreamReader inputReader = new InputStreamReader(inputStream); BufferedReader buffReader = new BufferedReader(inputReader); String line; StringBuilder builder = new StringBuilder(); try { while (( line = buffReader.readLine()) != null) { builder.append(line); builder.append("\n"); } } catch (IOException e) { return null; } return builder.toString(); } } Here is the error code:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.opimand.manul5, PID: 3325 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.opimand.manul5/com.example.opimand.manul5.DetailActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190) at android.content.res.ResourcesImpl.openRawResource(ResourcesImpl.java:298) at android.content.res.Resources.openRawResource(Resources.java:1239) at android.content.res.Resources.openRawResource(Resources.java:1184) at com.example.opimand.manul5.DetailActivity.readRawTextFile(DetailActivity.java:40) at com.example.opimand.manul5.DetailActivity.onCreate(DetailActivity.java:32) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)