简体   繁体   中英

E/AndroidRuntime: Error

Reduced errors by changing the theme back to Theme.AppCompat.Light. But, many errors still exist. I don't what's problem with the basic code in Android studio. It was working just fine few seconds before.

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);
    }

}

In a Textview you can't just setText() using an integer, you have to convert it to a string first.

try

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

As TextView doesn't accept Integer to be set as setText, it needed to convert to a string or append to a empty string.

tv.setText(""+num);

OR

tv.setText(String.valueOf(num));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM