我的应用程序正常工作但是当我添加启动画面时出现错误,我不知道该怎么做或如何解决它
错误是
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
我的MainActivity
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent splashIntent = new Intent(MainActivity.this , SplashActivity.class);
MainActivity.this.startActivity(splashIntent);
MainActivity.this.finish();
}
},SPLASH_DISPLAY_LENGTH);
// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);
// Set a click listener on that View
numbers.setOnClickListener(new OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
// Create a new intent to open the {@link NumbersActivity}
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);
// Start the new activity
startActivity(numbersIntent);
}
});
和activity_splash
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/category_phrases"
tools:context="com.example.android.miwok.SplashActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/nemo"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO NEMO"
android:textColor="#FFF"
android:textSize="40sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="40dp"
android:fontFamily="casual"
android:textStyle="bold|italic"
/>
SplashActivity
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
}
}
有什么可以解决的吗?
答案 0 :(得分:1)
您忘记在textview中设置ID
<TextView
android:id="@+id/numbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO NEMO"
android:textColor="#FFF"
android:textSize="40sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="40dp"
android:fontFamily="casual"
android:textStyle="bold|italic"
/>