2015-10-16 126 views
-4

當我單擊添加按鈕而不是打開學生的詳細信息時,它說應用程序已停止工作。 請幫助我。 謝謝。應用程序已停止工作

這是我的代碼:

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) 
+0

你的朋友Mr. Debugger告訴你什麼?首先應該在oncreate()方法中使用 – Bathsheba

+0

。之前問一個問題,請按照編碼步驟 –

+1

它是無關緊要的我怎麼寫它 –

回答

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是一個類,你不使用==比較 兩個實例類相當於使用equals方法。

因此,您的條件會進入其他部分...因爲它可能會引發一些錯誤,因爲其他部分中的操作可能沒有按照您的邏輯準備好。因爲你的添加點擊事件應該只執行,如果部分而不是其他人提到如此做的更改和檢查