Android 应用程序已停止工作

Android 应用程序已停止工作,android,android-studio,Android,Android Studio,当我点击“添加”按钮而不是打开学生详细信息时,它显示应用程序已停止工作。 请帮帮我。 多谢各位 这是我的代码: package com.example.rahul.myapplication; import android.app.ListActivity; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.supp

当我点击“添加”按钮而不是打开学生详细信息时,它显示应用程序已停止工作。 请帮帮我。 多谢各位

这是我的代码:

    package com.example.rahul.myapplication;

    import android.app.ListActivity;
    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.HashMap;

    public class MainActivity extends ListActivity  implements android.view.View.OnClickListener{

        Button btnAdd,btnGetAll;
        TextView student_Id;

        @Override
        public void onClick(View view) {
            if (view== findViewById(R.id.btnAdd)){

                Intent intent;
                intent = new Intent(this,StudentDetail.class);
                intent.putExtra("student_Id",0);
                startActivity(intent);

            }else {

                StudentRepo repo = new StudentRepo(this);

                ArrayList<HashMap<String, String>> studentList =  repo.getStudentList();
                if(studentList.size()!=0) {
                    ListView lv = getListView();
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                            student_Id = (TextView) view.findViewById(R.id.student_Id);
                            String studentId = student_Id.getText().toString();
                            Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
                            objIndent.putExtra("student_Id", Integer.parseInt( studentId));
                            startActivity(objIndent);
                        }
                    });
                    ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
                    setListAdapter(adapter);
                }else{
                    Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btnAdd = (Button) findViewById(R.id.btnAdd);
            btnAdd.setOnClickListener(this);

            btnGetAll = (Button) findViewById(R.id.btnGetAll);
            btnGetAll.setOnClickListener(this);

        }

        @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();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }







        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            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:fitsSystemWindows="true"
            tools:context=".MainActivity">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add"
                android:id="@+id/btnAdd"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@android:id/list"
                android:layout_centerHorizontal="true"
                android:layout_alignParentTop="true"
                android:layout_above="@+id/btnAdd" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="List All"
                android:id="@+id/btnGetAll"
                android:layout_alignParentBottom="true"
                android:layout_toRightOf="@+id/btnAdd" />

            <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
                android:layout_width="match_parent" 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>



            <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
                android:src="@android:drawable/ic_dialog_email" />

        </RelativeLayout>

将代码部分从

 @Override
    public void onClick(View view) {
        if (view== findViewById(R.id.btnAdd)){

            Intent intent;
            intent = new Intent(this,StudentDetail.class);

视图是一个类,您不需要使用==来比较 类而不是使用equals方法


因此,您的条件进入else部分…因为它可能会抛出一些错误,因为else部分中的操作可能没有按照您的逻辑准备好。因为add click事件应该只在part时执行,而不是前面提到的else,所以进行更改并检查

调试器先生告诉您的是什么?oncreate()方法应该是第一个。在提出问题之前,请遵循编码步骤。无论我如何编写,您可以添加logcat报告吗?我已经添加了logcat报告
 @Override
    public void onClick(View view) {
        if (view== findViewById(R.id.btnAdd)){

            Intent intent;
            intent = new Intent(this,StudentDetail.class);
 @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnAdd){

            Intent intent;
            intent = new Intent(MainActivity.this,StudentDetail.class);