0

The error, which I am getting:

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)*

the file 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();
    }

    ...
}

and the XML layout:

<?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>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216

1 Answers1

4

You are trying to find the editor in a wrong view.

Try looking for it in your root view, like this:

 editor = (Editor) findViewById(R.id.editor);
viplezer
  • 5,519
  • 1
  • 18
  • 25
  • 2
    This isn't inflating it, you're finding it. It was already inflated. Although you got the bug right. – Gabe Sechan Sep 18 '18 at 20:47
  • You are right, thanks for clarifying it! I am modifying the answer – viplezer Sep 18 '18 at 20:48
  • tried what u said.. still stuck editor = (Editor) findViewById(R.id.editor); //error here Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.verma.inspire.PostActivity.setUpEditor(PostActivity.java:50) at com.example.verma.inspire.PostActivity.onCreate(PostActivity.java:46) – cyberpunk922 Sep 18 '18 at 20:56
  • This is an other issue. We can't see your other views in your XML from the setUpEditor function (like action_h1), but I am suspecting that they are in your included layout. If so, try finding them with mToolbar.findViewById( ... ) – viplezer Sep 18 '18 at 20:59