gpt4 book ai didi

java - android studio findViewById-int 上的空指针异常

转载 作者:行者123 更新时间:2023-12-01 17:49:05 26 4
gpt4 key购买 nike

我收到的错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.github.irshulx.Editor.findViewById(int)' on a null object reference at com.example.verma.inspire.PostActivity.onCreate(PostActivity.java:45) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)*

文件PostActivity.java :

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.github.irshulx.models.EditorTextStyle;
import com.github.irshulx.Editor;

public class PostActivity extends AppCompatActivity {

private Toolbar mToolbar;
private Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);

mToolbar = (Toolbar) findViewById(R.id.update_post_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Update Post");

editor = (Editor) editor.findViewById(R.id.editor); //error here
setUpEditor();
}

...
}

和 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostActivity">

<include
android:id="@+id/update_post_page_toolbar"
layout="@layout/app_bar_layout" />

<com.github.irshulx.Editor
android:layout_width="match_parent"
android:id="@+id/editor"
app:render_type="Editor"
app:placeholder="Start writing here..."
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_height="wrap_content"
android:paddingBottom="100dp" />

</RelativeLayout>

最佳答案

您试图在错误的 View 中查找编辑器。

尝试在 Root View 中查找它,如下所示:

 editor = (Editor) findViewById(R.id.editor);

关于java - android studio findViewById-int 上的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com