Android java.lang.RuntimeException:无法启动活动(TextView为空)

Android java.lang.RuntimeException:无法启动活动(TextView为空),android,custom-font,Android,Custom Font,我正在尝试在我的android应用程序中添加自定义字体 LoginActivity.java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.content.Intent; import android.app.Activity; import android.widget.TextView; import android.graphics.Typef

我正在尝试在我的android应用程序中添加自定义字体

LoginActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.app.Activity;
import android.widget.TextView;
import android.graphics.Typeface;

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button login = (Button) findViewById(R.id.login);
        Button signup = (Button) findViewById(R.id.signup);
        TextView username = (TextView) findViewById(R.id.username);
        Typeface custom_font = Typeface.createFromAsset(this.getAssets(), "fonts/smartwatch.ttf");
        username.setTypeface(custom_font);
        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent loginOnlyIntent = new Intent(view.getContext(), LoginOnlyActivity.class);
                startActivity(loginOnlyIntent);
            }
        });


        signup.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent signUpIntent = new Intent(view.getContext(), Signup.class);
                startActivity(signUpIntent);
            }
        });


    }
}
我得到一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.LoginActivity}: java.lang.NullPointerException
我也在
src/main
文件夹中添加了
assests
文件夹。在
assests
文件夹中是
font
文件夹,其中有字体文件
smartwatch.tff

我已经检查了几乎所有可用的答案,但到目前为止,没有一个对我有效

更新: 在使用调试器之后,我发现

TextView username = (TextView) findViewById(R.id.username);
返回null,因此返回NPE

添加布局文件:

 <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="155.0dip"
            android:paddingTop="200.0dip"
            android:paddingRight="4.0dip"
            android:paddingBottom="1.0dip"
            android:layout_below="@+id/TextView02">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/UserName"
                android:id="@+id/username"/>
            <EditText
                android:id="@+id/edit_phone"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:inputType="number"
                android:hint="  "/>
 </LinearLayout>


请告诉我为什么textview用户名为空

您的代码对我来说很好。


在LinearLayout中,您提供了缺少的按钮,但我认为您已将它们添加到项目中。此外,我还有一个错误,因为您省略了XML名称空间

xmlns:android="http://schemas.android.com/apk/res/android"
线性布局

尝试使项目无效并重新启动,这很奇怪TextView上的空指针问题,您做得很好,除非问题出在其他地方

确保已创建资产文件夹,执行以下操作:
右键单击“应用程序”文件夹,新建-->文件夹-->资产文件夹

您的代码对我来说运行良好。


在LinearLayout中,您提供了缺少的按钮,但我认为您已将它们添加到项目中。此外,我还有一个错误,因为您省略了XML名称空间

xmlns:android="http://schemas.android.com/apk/res/android"
线性布局

尝试使项目无效并重新启动,这很奇怪TextView上的空指针问题,您做得很好,除非问题出在其他地方

确保已创建资产文件夹,执行以下操作:
右键单击“应用程序”文件夹,新建-->文件夹-->资产文件夹

请使用完整日志编辑。。。不仅仅是异常原因,您显示的XML是
activity\u login.XML
?如果不是,这就解释了为什么为空。您是否在活动的
onCreate
方法中扩展了正确的布局文件?关于
TextView
可能为空,我看不到任何其他原因…@cricket\u 007是的,它是
activity\u login.xml
请显示完整的xml。这两个按钮在哪里?请用完整的日志编辑。。。不仅仅是异常原因,您显示的XML是
activity\u login.XML
?如果不是,这就解释了为什么为空。您是否在活动的
onCreate
方法中扩展了正确的布局文件?关于
TextView
可能为空,我看不到任何其他原因…@cricket\u 007是的,它是
activity\u login.xml
请显示完整的xml。这两个按钮在哪里?缺少名称空间,因为显示的只是XML布局的示例。缺少名称空间,因为显示的只是XML布局的示例