0
目前,我正在學習用碎片的工作,到目前爲止,我已經相當成功,但已經提出了以下錯誤:誤差片段
10-14 15:24:55.309 21831-21831/? E/AndroidRuntime: FATAL EXCEPTION: main
10-14 15:24:55.309 21831-21831/? E/AndroidRuntime: Process: com.example.wq.myapp, PID: 21831
10-14 15:24:55.309 21831-21831/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
10-14 15:24:55.309 21831-21831/? E/AndroidRuntime: at com.example.wq.myapp.PhoneMessages.onActivityCreated(PhoneMessages.java:44)
如果任何人都可以解釋我做錯了什麼這將不勝感激:)
這裏是我的代碼片段:
package com.example.wq.myapp;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class PhoneMessages extends Fragment {
ListView lvMsgList;
// Cursor Adapter
SimpleCursorAdapter adapter1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_phone_messages, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://mms-sms/conversations");
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor a = getActivity().getContentResolver().query(inboxURI, new String[]{"*"}, null, null, "normalized_date desc");
// Attach Cursor with adapter and display in listView
adapter1 = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.row, a,
new String[]{"body", "date", "address", "thread_id"},
new int[]{R.id.lblMsg, R.id.lblDate, R.id.lblNumber, R.id.lblID}, 0);
lvMsgList.setAdapter(adapter1);
//set OnItemClickListener for listView, start ConvListActivity
lvMsgList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView TVConvID = (TextView) view.findViewById(R.id.lblID);
String ConvID = TVConvID.getText().toString();
Intent i = new Intent(getActivity(), ConvListActivity.class);
i.putExtra("CID", ConvID);
startActivity(i);
}
});
getActivity().setTitle("Phone Messages");
}
}
非常感謝你,我花了很多時間試圖弄清楚:) –
沒問題樂意幫忙 – Eoin