该应用程序已停止工作

时间:2015-10-16 06:58:14

标签: android android-studio

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

这是我的代码:

    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>

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

10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime: Process: com.example.rahul.myapplication, PID: 15064
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.rahul.myapplication/com.example.rahul.myapplication.StudentDetail}; have you declared this activity in your AndroidManifest.xml?
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3917)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3877)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Activity.startActivity(Activity.java:4200)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.Activity.startActivity(Activity.java:4168)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at com.example.rahul.myapplication.MainActivity.onClick(MainActivity.java:52)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-16 13:51:51.310 15064-15064/com.example.rahul.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

更改您的代码部分
 @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);
  

View是一个类,你不使用==来比较两个实例   类而不是等于方法。

因此你的条件进入了else部分...因为它可能会抛出一些错误,因为根据你的逻辑,else部分的操作可能没有准备好。因为你的添加点击事件应该只在部分执行,而不是如上所述,所以进行更改并检查