activity_main - >时setContentView崩溃习惯

时间:2015-11-13 04:57:29

标签: java android xml genymotion

我知道有很多关于使用setContentView时崩溃的问题,但我还没有发现任何像我一样基本的问题。

这基本上是我使用Android Studio的第一天。我现在正在关注Udemy的Paulo Dichone的Android课程。他让我们做的第一件事就是将起始布局改为我们自己制作的布局。即使改变不到一行也会导致我的模拟程序在Genymotion中崩溃。

以下是MainActivity.java的代码。我从原始(工作)改变的唯一的东西是activity_main作为setContentView的参数(在onCreate的第二行),我改为mylayout。

package welcomeandroid.myandroid.com.welcomeandroid;

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;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);
    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代码' mylayout'如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Please work"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal" />

运行程序的logcat消息如下:

11-12 23:28:17.584 2244-2244/? I/art: Late-enabling -Xcheck:jni
11-12 23:28:18.026 2244-2244/welcomeandroid.myandroid.com.welcomeandroid      D/AndroidRuntime: Shutting down VM
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid  E/AndroidRuntime: FATAL EXCEPTION: main
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: Process: welcomeandroid.myandroid.com.welcomeandroid, PID:  2244
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{welcomeandroid.myandroid.com.welcomeandroid/welcomeandroid.myandroid.com.welcomeandroid.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at welcomeandroid.myandroid.com.welcomeandroid.MainActivity.onCreate(MainActivity.java:19)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
11-12 23:28:18.028 2244-2244/welcomeandroid.myandroid.com.welcomeandroid E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
11-12 23:28:22.531 2244-2244/welcomeandroid.myandroid.com.welcomeandroid I/Process: Sending signal. PID: 2244 SIG: 9

我真的很感激任何帮助。在指导员指示的方式下只改变了一小部分代码,这有点令人沮丧。

2 个答案:

答案 0 :(得分:2)

您必须从代码中删除以下行:

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

因为您的布局中没有工具栏和FloatingActionButton。

答案 1 :(得分:0)

因为迈克说您的布局没有工具栏或FloatingActionButton。试试这个可以帮助你的XML

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" 
     android:layout_width="match_parent"
     android:layout_height="match_parent">   

     <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:text="Please work"
       android:id="@+id/textView2"
       android:layout_gravity="center_horizontal" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" 
         />

    </android.support.design.widget.AppBarLayout>

   <android.support.design.widget.FloatingActionButton

      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="right|bottom"
      android:layout_marginBottom="@dimen/activity_vertical_margin"
      android:layout_marginRight="@dimen/activity_horizontal_margin"
      android:src="@drawable/ic_refresh_white_36dp"
      app:borderWidth="0dp" />
    </LinearLayout>

祝你好运