android studio上的Java错误

时间:2015-12-08 18:51:44

标签: java android xml android-layout

我是Android应用程序开发的新手我正在关注buckys教程 目前我就是这个部分 https://www.youtube.com/watch?v=MdOxtws4DsI&index=13&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl 我做了所有的事情,但是当我运行应用程序时,我发现此错误是我的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

这里也是我编辑的java文件

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);
    }
}

最后是xml文件

<?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>

2 个答案:

答案 0 :(得分:0)

您实际上在XML中没有引用工具栏的元素。你需要一个带有ID&#34;工具栏的元素&#34;也许是这样的:

<?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>

编辑:

此外,使用这些行,您将Activity内容设置为XML定义布局之外的内容:

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

您应该更改XML主RelativeLayout并为其指定ID(如其中的元素),然后像这样设置内容视图:

setContentView(R.layout.main_layout);

答案 1 :(得分:0)

我还没有使用工具栏,所以我可以关闭,但是你用一个通常对应于XML元素的R.id来实例化你的工具栏。