0
votes

I would be so, so grateful if you can help me with this problem.

I am learning android development with Android Basics on Udacity. I am at User Input: Lesson 8. The apps have been downloading to my phone fine so far but now the app (JustJava) crashes when I press the only button in it. There are no serious (red) errors showing on Android Studio.

I have reviewed logcat but cannot understand it except that the button (onClick) is the problem. I have no idea how to fix it.

This keeps appearing in red in logcat every few seconds: E/TZ_CCM_SERVER: Only 'CCM' are supported

Here is the full error log relating my runtime crash:

07-09 10:51:17.638 18253-18253/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.justjava, PID: 18253 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10826) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) at android.view.View.performClick(View.java:5697)  at android.widget.TextView.performClick(TextView.java:10826)  at android.view.View$PerformClick.run(View.java:22526)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7224)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.android.justjava.MainActivity.displayPrice(MainActivity.java:45) at com.example.android.justjava.MainActivity.submitOrder(MainActivity.java:28) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)  at android.view.View.performClick(View.java:5697)  at android.widget.TextView.performClick(TextView.java:10826)  at android.view.View$PerformClick.run(View.java:22526)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7224)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

EDIT: MainActivity / java code:

    package com.example.android.justjava;

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

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int coffeeNumber = 3;
        display(coffeeNumber);
        displayPrice(coffeeNumber * 5);
    }


    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }


}

XML Code:

<?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:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
    android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="16sp"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:id="@+id/quantity_text_view"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:padding="10dp"
        android:textAllCaps="true"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="€0"
        android:textSize="16sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/black"
        android:padding="10dp"
        android:layout="@+id/price_text_view"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Order"
        android:onClick="submitOrder"
        />


</LinearLayout>
3
OMG, please, dont use onClick from xmlAnton A.
Provide MainActivity.java and activity_main.xml code. Thanks.Tomas Jablonskis
I have added them both now. Thank you TomasWarriorMonkIRE

3 Answers

0
votes

Check your activity_main.xml in this layout you does not have a TextView with that id price_text_view. Just define a TextView with that id and your App will not crash anymore and you can go Forward to the next chapter ;)

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

I think you just made a silly misstake. I saw you're using layout insteed of id ^^

0
votes

Change your xml like below

<?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:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="16sp"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:id="@+id/quantity_text_view"/>

<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Price"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="€0"
    android:textSize="16sp"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:padding="10dp"
    android:layout="@+id/price_text_view"
    />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Order"
    android:onClick="submitOrder"
    />

0
votes

I have just Called TextView in global, then initialized in onCreateand then using wherever we need.

Just replace this edited java class,

public class MainActivity extends AppCompatActivity {
         TextView priceTextView, quantityTextView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    priceTextView = (TextView) findViewById(R.id.price_text_view);
    }



    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int coffeeNumber = 3;
        display(coffeeNumber);
        displayPrice(coffeeNumber * 5);
    }


    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        quantityTextView.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {

        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }


}