Android App Emulator问题

时间:2016-01-15 02:47:35

标签: java android android-activity mobile

我在Android Studio中创建了一个项目: package com.example.apps.newapplication; Java代码:

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 DakotasLayout = new RelativeLayout(this);
        Button redButton = new Button(this);

        DakotasLayout.addView(redButton);
        setContentView(DakotasLayout);

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

Manifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app.newapplication">

    <application>
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

问题是每次我在模拟器中启动它时我的应用程序都会关闭。 我不确定我的java文件中有什么问题。可能是我的清单文件错了,但我不确定。 这是logcat:

Error Logs:
01-13 18:24:51.244 4486-4486/? I/art: Not late-enabling -Xcheck:jni (already on)
01-13 18:24:51.328 4486-4494/? I/art: Debugger is no longer active
01-13 18:24:51.369 4486-4494/? W/art: Suspending all threads took: 41.636ms
01-13 18:24:51.373 4486-4486/? W/System: ClassLoader referenced unknown path: /data/app/com.example.apps.newapplication-2/lib/x86
01-13 18:24:51.423 4486-4486/? D/AndroidRuntime: Shutting down VM
01-13 18:24:51.424 4486-4486/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.apps.newapplication, PID: 4486
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apps.newapplication/com.example.apps.newapplication.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.example.apps.newapplication.MainActivity.onCreate(MainActivity.java:28)
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)
01-13 18:25:03.649 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 60.192ms
01-13 18:25:22.653 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 44.409ms
01-13 18:25:29.152 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 24.137ms

1 个答案:

答案 0 :(得分:0)

问题是您没有为活动期望的内容设置有效的布局。您的布局需要在每个findViewById(...);的调用中都有一个定义的视图。视图的ID将与您传入方法的内容相匹配。

目前,您只是将内容视图设置为带有按钮的相对布局,因此当您使用NullPointerException查找其他视图并稍后尝试使用它们时,您可能会获得findViewById(...);

res/layout目录中查找有效的布局文件,并将其用作内容视图,因为这看起来像是一个模板。

setContentView(R.layout.activity_main);