-1

Hello i just started code, i built a counter app but its not running on either my emulator or my android phone. Any help please

This is the xml code (activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="16dp"
                android:text="@string/team_a"
                android:textColor="@color/colorAccent"
                android:textSize="14sp"
                app:fontFamily="cursive" />

            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingBottom="24dp"
                android:text="@string/_0"
                android:textColor="@color/colorAccent"
                android:textSize="56sp"
                app:fontFamily="cursive" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addThreeForTeamA"
                android:text="@string/_3_points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addTwoForTeamA"
                android:text="@string/_2_points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addOneForTeamA"
                android:text="@string/free_throw" />
        </LinearLayout>
        <view
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp"
            android:background="@android:color/holo_blue_bright" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="cursive"
                android:gravity="center"
                android:padding="16dp"
                android:text="@string/team_b"
                android:textColor="#FF1C25C8"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="cursive"
                android:gravity="center"
                android:paddingBottom="24dp"
                android:text="@string/_0"
                android:textColor="#FF1C25C8"
                android:textSize="56sp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addThreeForTeamB"
                android:text="@string/_3_points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addTwoForTeamB"
                android:text="@string/_2_points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginBottom="8dp"
                android:onClick="addOneForTeamB"
                android:text="@string/free_throw" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="8dp"
        android:onClick="resetScore"
        android:text="@string/reset" />
</RelativeLayout>

The Java code is below (MainActivity.java)

package com.example.monsurah.counter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.monsurah.counter.R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    /**
     *Adds one for Team A
     */
    public void addOneForTeamA (View v) {
        scoreTeamA = scoreTeamA + 1;
        displayForTeamA(scoreTeamA);
    }
    /**
     *Adds two for Team A
     */
    public void addTwoForTeamA (View v) {
        scoreTeamA = scoreTeamA + 2;
        displayForTeamA(scoreTeamA);
    } /**
     *Adds three for Team A
     */
    public void addThreeForTeamA (View v) {
        scoreTeamA = scoreTeamA + 3;
        displayForTeamA(scoreTeamA);
    } /**
     *Adds one for Team B
     */
    public void addOneForTeamB (View v) {
        scoreTeamB = scoreTeamB + 1;
        displayForTeamB(scoreTeamB);
    } /**
     *Adds two for Team B
     */
    public void addTwoForTeamB (View v) {
        scoreTeamB = scoreTeamB + 2;
        displayForTeamB(scoreTeamB);
    } /**
     *Adds three for Team B
     */
    public void addThreeForTeamB (View v) {
        scoreTeamB = scoreTeamB + 3;
        displayForTeamB(scoreTeamB);
    }
    /**
     *Reset Scores
     */
    public void resetScore (View v) {
        scoreTeamA = 0;
        scoreTeamB = 0;
        displayForTeamB(scoreTeamB);
        displayForTeamB(scoreTeamB);
    }
    /**
     * Displays the given score for Team A.
     */
    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }
    /**
     * Displays the given score for Team B.
     */
    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }
}

The Error message im getting is

Caused by: android.view.InflateException: Binary XML file line #68: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) 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.rInflate(LayoutInflater.java:873) 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.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.monsurah.counter.MainActivity.onCreate(MainActivity.java:16) at android.app.Activity.performCreate(Activity.java:6942) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3018) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6724) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mubby
  • 1
  • 1
  • @MartinZeitler It is not duplicate, as you can see it is internal crash. Not crash of User code. – Khemraj Sharma Oct 09 '18 at 07:40
  • 1
    @Khemraj it reads `Caused by: java.lang.NullPointerException`... whatever `line 68` of the XML may be (sadly there are no line-numbers on here). – Martin Zeitler Oct 09 '18 at 07:42
  • @MartinZeitler Can you find below solution in suggestion duplicate answer? You are from Android, see this issue does not come from PO java code. – Khemraj Sharma Oct 09 '18 at 07:44
  • @Khemraj using `Space` might rather be common there... while such minor typographical errors are also a good reason to close a question. – Martin Zeitler Oct 09 '18 at 07:47
  • @MartinZeitler right solution is not there in duplicate marked question. So please read always error before marking duplicate. There can be many ways to implement same thing. – Khemraj Sharma Oct 09 '18 at 07:48
  • @Khemraj it isn't: https://developer.android.com/reference/android/widget/Space (admittedly, it does nothing else, than en empty `View`; while it is rather readable) – Martin Zeitler Oct 09 '18 at 07:49
  • @MartinZeitler you are right, it was `androidx.legacy.widget.Space`, but final discussion is that it is not duplicate question. – Khemraj Sharma Oct 09 '18 at 07:50
  • 1
    it's duplicate of `NPE` and also `android.view.InflateException: Binary XML file`... you still could have your answer accepted, but no new answers can be added (they all pretend "my question is something else" - while in 90% of the cases, it isn't). – Martin Zeitler Oct 09 '18 at 07:54
  • @MartinZeitler The very simple logic behind marking duplicate is to tell user solution but this does not answer current question. Hope you understand. – Khemraj Sharma Oct 09 '18 at 08:10
  • 1
    @Khemraj I not only marked it duplicate, but also hinted for `line 68` - because the idea might be to a) motivate people to learn reading error messages and b) learn searching before posting a new question (no matter how convenient it may appear to simply ask instead of trying to solve the issue)... and this question does not show any attempt doing so (overall, there are 2-3 plausible reasons to close it). – Martin Zeitler Oct 09 '18 at 08:16
  • @MartinZeitler Its late answer, just seen that Space is deprecated, I was talking about v4 Space, because all projects do not have widget package. https://developer.android.com/reference/android/support/v4/widget/Space – Khemraj Sharma Oct 10 '18 at 17:55
  • @Khemraj it's base SDK, available in any project >= minimum API level 14. – Martin Zeitler Oct 10 '18 at 18:04
  • @Khemraj You should comment this question HERE, not on other posts. And... what has this account to do with you? Anyway, EVERY NPE IS THE SAME STORY. Therefore, a duplicate. – Phantômaxx Oct 10 '18 at 18:28
  • @KlingKlang yes every NPE has same story, but what can this guy do with internal class NPE? thus it is not duplicate, you can see answer below, that was cause of this issue. – Khemraj Sharma Oct 11 '18 at 05:04

1 Answers1

0

Change view to View in your layout.

    <view
        android:layout_width="1dp"
        ... />

to

    <View
        android:layout_width="1dp"
        ... />

Suggestion: See @myanswer for one line solution.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212