致命异常:main(无法转换为维度)

时间:2015-08-29 06:30:28

标签: java android xml

我对android以及java编程都很新。我无法运行我的第一个应用程序,因为它说“不幸的是应用程序停止了”。在logcat中我可以看到“FATAL EXCEPTION:main”。你能否请注意我正在制作的菜鸟错误。我的java代码如下:

    package com.example.calculator;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    int counter;
    Button add, sub;
    TextView out;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle(R.string.app_name);
            setSupportActionBar(toolbar);
        }
        counter = 0;
        add = (Button) findViewById(R.id.button1);
        sub = (Button) findViewById(R.id.button2);
        out = (TextView) findViewById(R.id.textView3);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            counter++;
            out.setText("Current value is "+counter);
            }
        });
sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            counter--;
            out.setText("Current value is "+counter);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的XML如下:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/Theme.AppCompat"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.calculator.MainActivity">


    <Button
        android:id="@+id/button2"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignEnd="@+id/textView2"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignStart="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginEnd="@+id/textView2"
        android:layout_marginRight="@+id/textView2"
        android:text="@string/sub"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/button2"
        android:layout_alignRight="@+id/button2"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button1"
        android:layout_below="@+id/button2"
        android:gravity="center"
        android:layout_marginTop="17dp"
        android:text="@string/out"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="@string/hello_world"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_alignEnd="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="32dp"
        android:text="@string/add"
        android:textSize="20sp" />

</android.support.v7.widget.Toolbar>

logcat的:

08-29 05:34:14.800: E/AndroidRuntime(770): FATAL EXCEPTION: main
08-29 05:34:14.800: E/AndroidRuntime(770): Process: com.example.calculator, PID: 770
08-29 05:34:14.800: E/AndroidRuntime(770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.os.Handler.dispatchMessage(Handler.java:102)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.os.Looper.loop(Looper.java:136)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread.main(ActivityThread.java:5026)
08-29 05:34:14.800: E/AndroidRuntime(770):  at java.lang.reflect.Method.invokeNative(Native Method)
08-29 05:34:14.800: E/AndroidRuntime(770):  at java.lang.reflect.Method.invoke(Method.java:515)
08-29 05:34:14.800: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
08-29 05:34:14.800: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-29 05:34:14.800: E/AndroidRuntime(770):  at dalvik.system.NativeStart.main(Native Method)
08-29 05:34:14.800: E/AndroidRuntime(770): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6134)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.app.ActionBar$LayoutParams.<init>(ActionBar.java:1327)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.widget.Toolbar$LayoutParams.<init>(Toolbar.java:1801)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.widget.Toolbar.generateLayoutParams(Toolbar.java:1694)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.widget.Toolbar.generateLayoutParams(Toolbar.java:107)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
08-29 05:34:14.800: E/AndroidRuntime(770):  at com.example.calculator.MainActivity.onCreate(MainActivity.java:23)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.Activity.performCreate(Activity.java:5242)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-29 05:34:14.800: E/AndroidRuntime(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
08-29 05:34:14.800: E/AndroidRuntime(770):  ... 11 more

2 个答案:

答案 0 :(得分:0)

您的布局使用不可用的主题属性。这些属性仅在API级别21及更高版本中可用。

我猜你在你的Manifest或gradle build config中设置的minSdkVersion低于21。

要与较低的API级别保持兼容,请将工具栏视图中的属性更改为:

android:background="@color/PrimaryColor"
android:minHeight="?android:attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

答案 1 :(得分:0)

您需要更改<Button .. xml。

使用任何marginEnd

更改marginRight@dimen

离。

    android:layout_marginEnd="@dimen/btnMarginEnd"
    android:layout_marginRight="@dimen/btnMarginRight"