错误是 引起:java.lang.NullPointerException:尝试调用虚方法' void android.widget.RelativeLayout.setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上
我的java是
package com.doctorfinderapp.doctorfinder;
import android.content.Intent;
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.widget.RelativeLayout;
public class DoctorProfileActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
//get data from previous activity
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, "Invia una mail a Dottore", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//vado sul feed
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
feedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(DoctorProfileActivity.this, FeedbackItemActivity.class);
startActivity(intent);
}
});
}
}
xml代码,其中声明了函数" feedbackfunction"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_marginTop="@dimen/activity_horizontal_margin"
android:padding="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.letsnurture.ln_202.coordinatorlayoutpart1.ScrollingActivity"
tools:showIn="@layout/activity_scrolling">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="@dimen/activity_horizontal_margin"
android:layout_weight="0.61">
<ImageView
android:id="@+id/ivContactItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_account_circle_black_24dp" />
<TextView
android:id="@+id/tvNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem1"
android:layout_toRightOf="@id/ivContactItem1"
android:text="Lorenzo Gitto"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<ToggleButton
android:id="@+id/chkState1"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_account_circle_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/ivContactItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_info_black_24dp"
android:visibility="visible" />
<TextView
android:id="@+id/tvNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem2"
android:layout_toRightOf="@id/ivContactItem2"
android:text="Chirurgia Toracica"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvNumber2"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="@id/ivContactItem2"
android:layout_toRightOf="@id/ivContactItem2"
android:text="Chirurgia Vascolare"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="@+id/chkState2"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_info_white_24dp"
android:checked="false"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/ivContactItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_book_black_24dp" />
<TextView
android:id="@+id/tvNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem3"
android:layout_toLeftOf="@+id/chkState3"
android:layout_toRightOf="@id/ivContactItem3"
android:layout_toStartOf="@+id/chkState3"
android:ellipsize="end"
android:maxLines="1"
android:text="Anni di esperienza: "
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvNumber3"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="@id/ivContactItem3"
android:layout_toRightOf="@id/ivContactItem3"
android:text="11"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="@+id/chkState3"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_info_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/ivContactItem4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_explore_black_24dp"
android:visibility="visible" />
<TextView
android:id="@+id/tvNumber4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem4"
android:layout_toLeftOf="@+id/chkState4"
android:layout_toRightOf="@id/ivContactItem4"
android:layout_toStartOf="@+id/chkState4"
android:ellipsize="end"
android:maxLines="1"
android:text="Luogo di lavoro"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvNumber4"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="@id/ivContactItem4"
android:layout_toRightOf="@id/ivContactItem4"
android:text="Via del Porto, 21"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="@+id/chkState4"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_info_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:onClick="feedbackfunction"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/ivContactItem5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_exit_to_app_white_24dp" />
<TextView
android:id="@+id/tvNumber5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem5"
android:layout_toLeftOf="@+id/chkState5"
android:layout_toRightOf="@id/ivContactItem5"
android:layout_toStartOf="@+id/chkState5"
android:ellipsize="end"
android:maxLines="1"
android:text="Punteggio Feedback"
android:clickable="true"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121"
/>
<ToggleButton
android:id="@+id/chkState5"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_book_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/ivContactItem6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_book_black_24dp"
android:visibility="visible" />
<TextView
android:id="@+id/tvNumber6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_toEndOf="@id/ivContactItem6"
android:layout_toLeftOf="@+id/chkState6"
android:layout_toRightOf="@id/ivContactItem6"
android:layout_toStartOf="@+id/chkState6"
android:ellipsize="end"
android:maxLines="1"
android:text="Ulteriori info"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvNumber6"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="@id/ivContactItem6"
android:layout_toRightOf="@id/ivContactItem6"
android:maxLines="10"
android:text="Vai Su linkedin per vedere le mie info e competenze...http://www.linkedin.com/NomeDottore "
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="@+id/chkState6"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/ic_exit_to_app_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="@layout/item_divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prenota appuntamento"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:text="Videochiama"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
有人能帮帮我吗?感谢所有
答案 0 :(得分:1)
你没有id等于反馈的任何RelativeLayout。
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
因此,您的反馈对象为空,并且您无法在空对象上执行onClick事件
解决方案
设置RelativeLayout id = feedback
答案 1 :(得分:0)
您似乎没有为@id
设置RelativeLayout
:
android:id="@+id/feedback"