-1

My Problem: application not starting.

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 info.androidhive.materialtabs.activity.MainActivity.onCreate(MainActivity.java:25)

Main activity line 25 , rellay_timeline.setOnClickListener(new
View.OnClickListener() {

MainActivity.java class:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    RelativeLayout rellay_timeline;
    private Toolbar toolbar;
    private Button btnSimpleTabs, btnScrollableTabs, btnIconTextTabs, btnIconTabs, btnCustomIconTextTabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rellay_timeline = findViewById(R.id.rellay_timeline999);

        rellay_timeline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Activity_Timeline.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            }
        });



        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        btnSimpleTabs = (Button) findViewById(R.id.btnSimpleTabs);
        btnScrollableTabs = (Button) findViewById(R.id.btnScrollableTabs);
        btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
        btnIconTabs = (Button) findViewById(R.id.btnIconTabs);
        btnCustomIconTextTabs = (Button) findViewById(R.id.btnCustomIconTabs);

        btnSimpleTabs.setOnClickListener(this);
        btnScrollableTabs.setOnClickListener(this);
        btnIconTextTabs.setOnClickListener(this);
        btnIconTabs.setOnClickListener(this);
        btnCustomIconTextTabs.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSimpleTabs:
                startActivity(new Intent(MainActivity.this, SimpleTabsActivity.class));
                break;
            case R.id.btnScrollableTabs:
                startActivity(new Intent(MainActivity.this, ScrollableTabsActivity.class));
                break;
            case R.id.btnIconTextTabs:
                startActivity(new Intent(MainActivity.this, IconTextTabsActivity.class));
                break;
            case R.id.btnIconTabs:
                startActivity(new Intent(MainActivity.this, IconTabsActivity.class));
                break;
            case R.id.btnCustomIconTabs:
                startActivity(new Intent(MainActivity.this, CustomViewIconTextTabsActivity.class));
                break;
        }
    }
}

Below is xml file :

<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"
tools:context="info.androidhive.materialtabs.activity.ScrollableTabsActivity">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginTop="372dp"
            android:orientation="horizontal"
            android:weightSum="3">

            <RelativeLayout
                android:id="@+id/rellay_timeline999"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorBitterSweet"
                android:clickable="true">

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="25dp"
                    android:gravity="center_vertical"
                    android:src="@drawable/ic_icon_archary"
                    android:tint="@color/colorWhite" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    android:background="@color/colorBitterSweetDark"
                    android:padding="8dp"
                    android:text="Stok Transferi"
                    android:textAlignment="center"
                    android:textColor="@color/colorWhite"
                    android:textSize="14sp" />

            </RelativeLayout>

activity_timeline

public class Activity_Timeline extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
drome
  • 7
  • 3
  • refer https://stackoverflow.com/questions/30508258/attempt-to-invoke-virtual-method-void-android-widget-button-setonclicklistener?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Ali Jun 02 '18 at 09:05
  • Where is your remaining button which you initialised after this error? Are you sure you are using correct layout file? – Ved Jun 02 '18 at 09:18
  • app has stopped error, If I delete the click command everything works. – drome Jun 02 '18 at 09:26
  • The XML file that you provided here, is it `activity_main.xml`? – Suleyman Jun 02 '18 at 09:31
  • activity_main.xml menu link – drome Jun 02 '18 at 09:48
  • Please goto your res/layout/ find the directory activity_main and delete the activity_main.xml(v23) – Khaliq Izrail Dec 09 '21 at 08:48

3 Answers3

0

You should cast layout to Relative Layout. I want to suggest to write like this

 rellay_timeline = (RelativeLayout)findViewById(R.id.rellay_timeline999);
0

Make sure it is a RelativeLayout component , or the id you are finding is in the same View you are setting the content view .

0

There seems nothing wrong with the code and xml. Are you sure, the xml is the activity_main.xml? Because it says

tools:context="info.androidhive.materialtabs.activity.ScrollableTabsActivity"

If you're sure you're using the right layout file, you can try to clean and rebuild the project. Sometimes the generated R class gets messed up and then ids are wrong and that strange errors can occur.

EDIT

Another possible reason (from Mohammed Ali's posted link) would be, that you have different layouts for different API levels (e.g. in layout-v23) that is used when you run the app and that is different to the default layout.

Ridcully
  • 23,362
  • 7
  • 71
  • 86