任何人都可以帮我解决这个问题我不明白当我点击活动我的应用程序崩溃时到底出了什么问题我已经分配了按钮仍然显示我下面的错误?我无法在这里找到例外。我尝试通过更改按钮的ID。然后,它也没有用。在XML文件中,包括Button。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.niyanta.askforleave/com.example.niyanta.askforleave.Activity.EmployeeLeave}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2911)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2989)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6753)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.niyanta.askforleave.Activity.EmployeeLeave.onCreate(EmployeeLeave.java:88)
at android.app.Activity.performCreate(Activity.java:7026)
at android.app.Activity.performCreate(Activity.java:7017)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1231)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2864)
这是我的java代码.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_leave);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ImageView ivBack = toolbar.findViewById(R.id.ivBack);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
recyclerView = (RecyclerView) findViewById(R.id.employee_leave_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Employeeleavelist = new ArrayList<>();
if (!isConnectd(getApplicationContext())) {
Toast.makeText(this, "Check your network connection", Toast.LENGTH_SHORT).show();
} else {
Log.e("LOGIN", "CALL API");
getemployeelist();
}
btnApprove =(Button)findViewById(R.id.btnApprove) ;
btnApprove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
approved();
}
});
btnReject =(Button) findViewById(R.id.btnReject);
}
这是我的XML代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_marginTop="5dp"
>
<Button
android:id="@+id/btnApprove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/custom_button3"
android:text="Approve"
android:textAllCaps="false"
android:textColor="@color/white"
/>
<LinearLayout
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="@color/white"></LinearLayout>
<Button
android:id="@+id/btnReject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/custom_button2"
android:text="Reject"
android:textAllCaps="false"
android:textColor="@color/white"
android:onClick="ShowDialog"/>
答案 0 :(得分:0)
它是ImageView
而非toolbar
,您可以通过Toolbar
对象进行类型转换。从这里删除工具栏对象。
ImageView ivBack = toolbar.findViewById(R.id.ivBack);
并替换:
ImageView ivBack = (ImageView).findViewById(R.id.ivBack);
答案 1 :(得分:0)
你的错误是approved()
的编码,因为这个错误意味着什么都没有被解析或解析一个给出错误的空值,所以如果你能更新你的问题给我们看{{1} 1}}编码然后我们可以帮助
答案 2 :(得分:0)
检查findViewById返回值,它可能为null。
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
*
* @return The view if found or null otherwise.
*/
@SuppressWarnings("TypeParameterUnusedInFormals")
@Nullable
public abstract <T extends View> T findViewById(@IdRes int id);