-1

i am new to android app developing i am following buckys tutorials currently i am on this part https://www.youtube.com/watch?v=MdOxtws4DsI&index=13&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl i did everything as told but when i run the app i find this error here's my logcat

12-08 20:42:45.548 2465-2465/com.kimdevelopment.kimmmm I/art: Not late-enabling -Xcheck:jni (already on)
12-08 20:42:45.549 2465-2465/com.kimdevelopment.kimmmm I/art: Late-enabling JIT
12-08 20:42:45.552 2465-2465/com.kimdevelopment.kimmmm I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-08 20:42:45.630 2465-2465/com.kimdevelopment.kimmmm W/System: ClassLoader referenced unknown path: /data/app/com.kimdevelopment.kimmmm-1/lib/x86
12-08 20:42:45.696 2465-2465/com.kimdevelopment.kimmmm D/AndroidRuntime: Shutting down VM
12-08 20:42:45.696 2465-2465/com.kimdevelopment.kimmmm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kimdevelopment.kimmmm, PID: 2465
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kimdevelopment.kimmmm/com.kimdevelopment.kimmmm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
    at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
    at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
    at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
    at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
    at com.kimdevelopment.kimmmm.MainActivity.onCreate(MainActivity.java:27)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-08 20:42:54.014 2465-2465/? I/Process: Sending signal. PID: 2465 SIG: 9

also here's the java file that i edited

package com.kimdevelopment.kimmmm;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout kimsLayout = new RelativeLayout(this);
        Button redButton = new Button(this);
        kimsLayout.addView(redButton);
        setContentView(kimsLayout);


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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

and finally the xml file

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.kimdevelopment.kimmmm.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>
Mike
  • 4,550
  • 4
  • 33
  • 47
twix
  • 45
  • 2
  • 9
  • better than adding a link to another topic which is not my same problem you can just help if you can. – twix Dec 08 '15 at 18:55
  • it is the same problem. you need to read the error, figure out what is null, and figure out why it is null. which is what is explained in the linked question. – njzk2 Dec 09 '15 at 02:54

2 Answers2

0

You don't actually have an element in your XML that references the toolbar. You need an element with an ID "toolbar" maybe like this:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.kimdevelopment.kimmmm.MainActivity"
    tools:showIn="@layout/activity_main">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/blue"
        android:foreground="?android:attr/windowContentOverlay"
        app:theme="@style/Toolbar"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

EDIT:

Also, with these lines, you are setting the Activity content to something besides your XML defined layout:

    RelativeLayout kimsLayout = new RelativeLayout(this);
    Button redButton = new Button(this);
    kimsLayout.addView(redButton);
    setContentView(kimsLayout);

You should change your XML main RelativeLayout and give it an id (like the elements within it) and then set the content view like this:

setContentView(R.layout.main_layout);
Jim
  • 10,172
  • 1
  • 27
  • 36
  • but i didnt add or remove anything that can cause this i am using the Noactionbar theme,also i tried it and i still get the error – twix Dec 08 '15 at 19:06
  • I edited my response... hope it helps. – Jim Dec 09 '15 at 02:36
0

I haven't used tool bars that much so I could be off, but you're instantiating your tool bar with a R.id which usually corresponds to an XML element.

Tammytee
  • 592
  • 5
  • 9