2015-03-19 12 views
0

我有這個代碼找到網絡周圍,並決定嘗試在我的應用程序,如果它適用。單擊按鈕時,應該在列表視圖中顯示數據庫中的所有數據時顯示活動。Android - 應用程序崩潰時,單擊按鈕以顯示列表視圖的活動

下面的代碼:

MainActivity.java

public class MainActivity extends ListActivity { 

private ProgressDialog pDialog; 

// URL to get contacts JSON 
private static String url = "http://api.androidhive.info/contacts/"; 

// JSON Node names 
private static final String TAG_CONTACTS = "contacts"; 
private static final String TAG_ID = "id"; 
private static final String TAG_NAME = "name"; 
private static final String TAG_EMAIL = "email"; 
private static final String TAG_ADDRESS = "address"; 
private static final String TAG_GENDER = "gender"; 
private static final String TAG_PHONE = "phone"; 
private static final String TAG_PHONE_MOBILE = "mobile"; 
private static final String TAG_PHONE_HOME = "home"; 
private static final String TAG_PHONE_OFFICE = "office"; 

// contacts JSONArray 
JSONArray contacts = null; 

// Hashmap for ListView 
ArrayList<HashMap<String, String>> contactList; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main5); 

    contactList = new ArrayList<HashMap<String, String>>(); 

    ListView lv = getListView(); 

    // Listview on item click listener 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)) 
        .getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.email)) 
        .getText().toString(); 
      String description = ((TextView) view.findViewById(R.id.mobile)) 
        .getText().toString(); 

      // Starting single contact activity 
      Intent in = new Intent(getApplicationContext(), 
        SingleContactActivity.class); 
      in.putExtra(TAG_NAME, name); 
      in.putExtra(TAG_EMAIL, cost); 
      in.putExtra(TAG_PHONE_MOBILE, description); 
      startActivity(in); 

     } 
    }); 

    // Calling async task to get json 
    new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(MainActivity5.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String id = c.getString(TAG_ID); 
        String name = c.getString(TAG_NAME); 
        String email = c.getString(TAG_EMAIL); 
        String address = c.getString(TAG_ADDRESS); 
        String gender = c.getString(TAG_GENDER); 

        // Phone node is JSON Object 
        JSONObject phone = c.getJSONObject(TAG_PHONE); 
        String mobile = phone.getString(TAG_PHONE_MOBILE); 
        String home = phone.getString(TAG_PHONE_HOME); 
        String office = phone.getString(TAG_PHONE_OFFICE); 

        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_ID, id); 
        contact.put(TAG_NAME, name); 
        contact.put(TAG_EMAIL, email); 
        contact.put(TAG_PHONE_MOBILE, mobile); 

        // adding contact to contact list 
        contactList.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       MainActivity5.this, contactList, 
       R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL, 
         TAG_PHONE_MOBILE }, new int[] { R.id.name, 
         R.id.email, R.id.mobile }); 

     setListAdapter(adapter); 
    } 

} 

} 

那麼這裏的地方按鈕位於類:

public class MainActivity3Activity extends Activity implements OnClickListener { 

Button ViewGradesActivity; 
Button ClassScheduleActivity; 
Button CalendarOfActivitiesActivity; 
Button AccountBalanceActivity; 
Button StudentProfileActivity; 

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


    ShowMainActivity= (Button) findViewById(R.id.mainactbutton); 
    ShowMainActivity.setOnClickListener(this); 

    ClassScheduleActivity = (Button) findViewById(R.id.classSched); 
    ClassScheduleActivity.setOnClickListener(this); 

    CalendarOfActivitiesActivity = (Button) findViewById(R.id.activities); 
    CalendarOfActivitiesActivity.setOnClickListener(this); 

    AccountBalanceActivity = (Button) findViewById(R.id.balance); 
    AccountBalanceActivity.setOnClickListener(this); 

    StudentProfileActivity = (Button) findViewById(R.id.profile); 
    StudentProfileActivity.setOnClickListener(this); 

} 

@Override 
public void onClick(View view) { 
    switch(view.getId()) { 
     //this is to show the activity with the list view 
     case R.id.mainactbutton: 
      startActivity(new Intent(this,MainActivity5.class)); 
      break; 
     case R.id.classSched: 
      startActivity(new Intent(this,classSchedule.class)); 
      break; 
     case R.id.activities: 
      startActivity(new Intent(this,CalendarOfActivities.class)); 
      break; 
     case R.id.balance: 
      startActivity(new Intent(this,AccountBalance.class)); 
      break; 
     case R.id.profile: 
      startActivity(new Intent(this,StudentProfile.class)); 
      break; 
    } 
} 

} 

所以每當我單擊mainactbutton應用程序崩潰。我該怎麼辦?列表視圖的活動有什麼問題嗎?

謝謝你的幫助。

編輯:現在,我已經編輯清單文件並添加MainActivity5應用仍然墜毀,這裏的日誌:

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
     at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:277) 
     at android.app.Activity.setContentView(Activity.java:1881) 
     at com.example.kreshiathea.myfirstapp.MainActivity5.onCreate(MainActivity5.java:52) 
     at android.app.Activity.performCreate(Activity.java:5122) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270) 

這裏是我的列表視圖:

activity_main5.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
<!-- Main ListView 
    Always give id value as list(@android:id/list) 
--> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 

+1

請張貼logcat的聲明。 – Shadow 2015-03-19 06:18:03

+0

你可以添加logcat嗎? – mustafasevgi 2015-03-19 06:19:12

+0

錯誤日誌在哪裏? – Piyush 2015-03-19 06:19:14

回答

1

您必須聲明中AdnroidManifest.xml MainActivity5類文件


如果從ListActivity延伸,那麼就不要使用的setContentView()。您需要通過getListView()獲取默認列表視圖。

Listview listview=getListView(); 

<ListView 
android:id="@android:id/list" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 
+0

我編輯了清單文件,但它崩潰了。我發佈了上面的logcat。 – 2015-03-19 07:12:34

+0

@progRookie你可以添加哪個xml包含你的listview? – mustafasevgi 2015-03-19 07:16:58

+0

我已經發布了XML ...請檢查出來。 – 2015-03-19 07:26:31

1

更改代碼

this.startActivity(new Intent(this,MainActivity5.class)); 

& 你宣佈你的AndroidManifest.xml這個活動?

+0

我不這麼認爲是因爲這個問題。 – Piyush 2015-03-19 06:29:23

+0

讓她檢查它 – 2015-03-19 06:32:07

+0

所以現在的問題是與上下文? – Piyush 2015-03-19 07:01:35

2

有你宣佈你的AndroidManifest.xml這個活動?

您使用的每個活動都在你的清單

 <activity 
     android:name="com.example.kreshiathea.myfirstapp.MainActivity5" 
     android:label="@string/app_name" 
     android:id="@android:id/list" 
     > 
     </activity> 
+0

我編輯了清單文件,但它崩潰了。我發佈了上面的logcat。 – 2015-03-19 07:12:26

+0

在xml文件中爲你的ListView設置'android:id =「@ android:id/list」'。 – Piyush 2015-03-19 07:14:37

+0

這也做了很大的幫助。謝謝。 – 2015-03-19 08:15:06

相關問題