我正在開發一個應用程序,它在ListFragment
中顯示一些基本解析的JSON。 我跟着http://www.androidhive.info/2012/01/android-json-parsing-tutorial,它在ListActivity
中工作得很好。然後我開始製作我自己的應用程序。我在Android Studio 1.5中創建了新項目,添加了Navigation Drawer Activity,因爲我有更多的東西要做/顯示並開始將代碼重寫爲片段。這基本上很容易,除了一個瘋狂的問題getApplicationContext()
和Cannot resolve method getApplciationContext()
錯誤仍然沒有得到解決。在ListFragment的ArrayList中JSON解析throwing getApplicationContext()錯誤
這裏是我的片斷的一部分:
public class Den1Fragment extends ListFragment {
private ProgressDialog pDialog;
// 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 View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_den1, container, false);
contactList = new ArrayList<>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new AdapterView.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(), // TODO error!
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();
}
如果我添加Intent in = new Intent(**getActivity().**getApplicationContext(), SingleContactActivity.class);
錯誤轉移到contactList = new ArrayList<>();
和Android Studio有不知道如何解決它,甚至谷歌搜索並沒有帶來多大的希望的結果。 我試圖用不同的重寫順序再次執行,發現如果我離開setContentView(R.layout.activity_main);
而不是將其更改爲return inflater.inflate(R.layout.fragment_den1, container, false);
,setContentView將變爲紅色,但contactList行保持「OK」。
編輯
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/content_frame"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
錯誤報告:
java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.ludato.festapp/cz.ludato.festapp.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at cz.ludato.festapp.Den1Fragment.onViewCreated(Den1Fragment.java:54)
酷。但現在我真的很奇怪的錯誤。代碼+ bugreport添加。 – tomaset22
嘗試將您的ListView的ID替換爲android:id =「@ + id/list」 – mgcaguioa
Lol。我只是意識到我需要將listview添加到fragment_den1.xml – tomaset22