Error when changing text source - android

Asked

Viewed 32 times

-1

Hello, I am creating a login app and I want to change the source of a textView but it is giving me the following error:

Attempt to invoke virtual method 'void android.widget.Textview.setTypeface(android.graphics.Typeface)' on a null Object Reference

XML:

<?xml version="1.0" encoding="utf-8"?>
    <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:background="@color/colorPrimary"
        tools:context=".Login">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"
            android:orientation="vertical">

            <TextView
                android:id="@+id/user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10sp"
                android:layout_marginTop="10sp"
                android:text="E-Mail ou NIF"
                android:textColor="#fff"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/mail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3sp"
                android:layout_marginTop="40sp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text=""
                android:textColor="#fff"
                android:textColorHighlight="#000" />

            <TextView
                android:id="@+id/pass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10sp"
                android:layout_marginTop="20sp"
                android:text="Password"
                android:textColor="#fff"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20sp"
                android:ems="10"
                android:inputType="textPassword"
                android:textColor="#fff"
                android:textColorHighlight="#000" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <Button
                    android:id="@+id/button"
                    android:layout_width="318dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20sp"
                    android:background="#09C"
                    android:onClick="entrar"
                    android:text="Entrar"
                    android:textColor="#fff" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="318dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10sp"
                    android:background="#09C"
                    android:text="Criar uma conta"
                    android:textColor="#fff" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvproc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#fff"
                android:textAlignment="center"
                android:layout_marginTop="200sp"
                android:textSize="30sp"
                android:text="A Processar" />

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyle"
                android:layout_marginTop="30sp"
                android:indeterminateTint="#fff"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>


    </RelativeLayout>

Java:

package imm.pt.immsmart;

    import android.content.Intent;
    import android.graphics.Typeface;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class Login extends AppCompatActivity {
        public String res;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            TextView tvu = (TextView) findViewById(R.id.user);
            Typeface font = Typeface.createFromAsset(getAssets(), "fonts/open.ttf");
            tvu.setTypeface(font);
            TextView tvp = (TextView) findViewById(R.id.pass);
            Typeface fontp = Typeface.createFromAsset(getAssets(), "fonts/open.ttf");
            tvp.setTypeface(fontp);
        }

    }
  • 1

    I see the code of onCreate() from another Activity and you will find what is missing in that. When you find the missing line try to understand why it is needed.

1 answer

0


You are not creating any link from your Activity with this xml. You need setContentView(id) on onCreate. It would look like this

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.meu_xml);
            TextView tvu = (TextView) findViewById(R.id.user);
            [...]
            
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.