尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)':fab

时间:2017-11-14 19:59:57

标签: java android nullpointerexception

我正在尝试创建一个带有侦听器的浮动按钮来打开一个新视图。

public class ConnectionMenu extends AppCompatActivity {
ConnectionManager conn = new ConnectionManager();
CredentialManager cred = new CredentialManager();
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_menu);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final EditText field1 = findViewById(R.id.editText);
    final EditText field2 = findViewById(R.id.editText2);
    final EditText field3 = findViewById(R.id.editText3);

    FloatingActionButton fab = findViewById(R.id.fab);
    Button button = findViewById(R.id.button2);
    final Spinner spinner = findViewById(R.id.spinner);
    final Intent intent = new Intent(this, CredentialMenu.class);
    Object[] credentials = cred.getAllnames();

    ArrayAdapter<Object> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_spinner_item, R.id.spinner, credentials);
    spinner.setAdapter(adapter);
    fab.setOnClickListener(new View.OnClickListener() { //fails here!!!!
        @Override
        public void onClick(View view) {
            startActivity(intent);
        }

但标题中的例外被抛出。我的xml文件的结构是:

activity_connection_menu.xml:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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="net.ddns.andrewnetwork.raspberrymonitor.View.ConnectionMenu">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

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

<include layout="@layout/content_connection_menu" />

content_connection_menu.xml

...
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="match_parent"
            android:layout_height="84dp"
            android:layout_marginLeft="32dp"
            android:layout_marginStart="32dp"
            android:layout_weight="1"
            android:clickable="true"
            android:focusable="true"
            app:backgroundTint="@color/colorPrimaryDark"
            app:srcCompat="@android:drawable/ic_menu_add" />

    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="40dp"
        android:layout_weight="1"
        android:text="@string/button1"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="206dp" />

</LinearLayout>

似乎 fab 变量已实例化,但其内容实际上为null。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

发布完整的错误。 或者至少有一个TextView,你没有初始化但在代码中的某处使用(最明显的错误指示行) 找到该文本视图并初始化它,即

tv = (TextView) findViewById(R.id.yourID);