E / AndroidRuntime:错误

时间:2016-09-09 16:18:37

标签: android android-studio onclick android-theme

通过将主题更改回Theme.AppCompat.Light来减少错误。但是,仍然存在许多错误。我不知道Android studio中的基本代码有什么问题。它在几秒钟之前工作得很好。

09-09 20:51:06.605 6986-6986/com.example.justjava E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.example.justjava, PID: 6986
                                                                Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
                                                                java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                    at android.view.View.performClick(View.java:5204)
                                                                    at android.view.View$PerformClick.run(View.java:21158)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: java.lang.reflect.InvocationTargetException
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                    at android.view.View.performClick(View.java:5204) 
                                                                    at android.view.View$PerformClick.run(View.java:21158) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                                    at android.content.res.Resources.getText(Resources.java:336)
                                                                    at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                    at android.widget.TextView.setText(TextView.java:4422)
                                                                    at com.example.justjava.MainActivity.display(MainActivity.java:25)
                                                                    at com.example.justjava.MainActivity.submitOrder(MainActivity.java:19)
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                    at android.view.View.performClick(View.java:5204) 
                                                                    at android.view.View$PerformClick.run(View.java:21158) 
                                                                    at android.os.Handler.handleCallback(Handler.java:739) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.example.justjava.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quantity"
        android:textAllCaps="true"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:textSize="16sp"
        android:textColor="@android:color/black"
        android:id="@+id/quantityView"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="@string/price"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:text="$0"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:textColor="@android:color/black"
        android:id="@+id/priceView"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/order"
        android:textSize="16sp"
        android:onClick="submitOrder"/>


</LinearLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

MainActivity.java

package com.example.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void submitOrder(View view){
        int numOfCoffees = 1;
        display(numOfCoffees);
        displayPrice(numOfCoffees*5);
    }

    private void display(int num){
        TextView tv1 = (TextView) findViewById(R.id.quantityView);
        tv1.setText(num);
    }

    private void displayPrice(int num){
        TextView tv2 = (TextView)findViewById(R.id.priceView);
        tv2.setText(num);
    }

}

3 个答案:

答案 0 :(得分:0)

在Textview中,你不能只使用整数setText(),你必须先将它转换为字符串。

tv1.setText(Integer.toString(num));

答案 1 :(得分:0)

由于TextView不接受将Integer设置为setText,因此需要转换为字符串或附加到空字符串。

  

tv.setText( “” + NUM);

OR

  

tv.setText(将String.valueOf(NUM));

答案 2 :(得分:0)

方法public void submitOrder(View v)是否在您正在为该布局充气的活动中定义?