I am trying to run an already written code, which should be ok. Yet I get the error.
I looked for similar problems but each problem and its solution are specific and don't give information that can be used for my problem. In other words, the exception message provided by Android Studio applies for endless possible problems.
the xml file that causes the problem - activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@string/contactsTtl"
android:textSize="@dimen/TtlTxtSize"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/lvContacts"
/>
</LinearLayout>
In the logcat I found that the exception is caused by
java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x4
Here is the logcat of the exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jsonreading, PID: 28802
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonreading/com.example.jsonreading.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Cant convert value at index 2 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:737)
at android.widget.TextView.<init>(TextView.java:1451)
at android.widget.TextView.<init>(TextView.java:1038)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.jsonreading.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
The MainActivity.java file (not the whole file but just what's needed to run and get the exception):
public class MainActivity extends AppCompatActivity {
Context context;
List<Contact> contactList;
ListView lvContacts;
ContactsAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Edit The question of which my question was suggested to be a possible duplicate of regards a problem caused by not saving values in the dimens.xml file in the values folder. My issue was an invalid value: instead of writing only a number in the dimens file, my file also had the units
If you look in the logcat that you posted down past the error that you identified you will see the following line:
Caused by: java.lang.UnsupportedOperationException: Cant convert value at index 2 to dimension: type=0x4
This is the real error. The error you see first is from a cascade of errors. In other words, this error occurred then the inflation failed.
If I define a dimension incorrectly, say, 8
instead of 8dp
:
<dimen name="test_dimen">8</dimen>
then I will get this error:
Caused by: android.view.InflateException: Binary XML file line #13: Can't convert value at index 7 to dimension: type=0x4
This is very similar to what you are seeing. Make sure your dimensions are valid: 8dp
, 8sp
, 8px
, etc. See the documentation for details.