i have been trying to solve a problem for few hours but i could not figured out .. i got the data from xml file online through Async task and called base adapter to fill those data in list view . but whenever i set the adapter to the list view it gives me some kind of Null pointer Exception Error and i could not find which object is null in this case!! i called getList from other class and players is not null as well... Thanks in advance .. Cheers!!!
MainActivity
package com.example.sandeshpoudel.lab13; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Adapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Button get_xml; private EditText Url_input; public final static String TAG = "my-filter"; private ListAdapter newAdapter; private ListView list_player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); get_xml = (Button)findViewById(R.id.xml_button); Url_input = (EditText) findViewById(R.id.url_input); list_player = (ListView) findViewById(R.id.list_player); get_xml.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Fetch_Xml().execute(Url_input.getText().toString()); Log.i(TAG,"fetching xml data from Url "+Url_input.getText().toString()); } }); } public void getList(List<Player>players){ newAdapter =new AdapterExample(this,players); list_player.setAdapter(newAdapter); } } Adapter Class
package com.example.sandeshpoudel.lab13; import android.app.Activity; import android.content.Context; import android.inputmethodservice.Keyboard; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.TextView; import java.util.List; public class AdapterExample extends BaseAdapter { private Context context; private List<Player> playerList; private LayoutInflater myInflatter; public AdapterExample(Context context,List<Player>myList){ super(); this.context = context; this.playerList= myList; Log.i(MainActivity.TAG,"created new adapter"); } @Override public int getCount() { return playerList.size(); } @Override public Object getItem(int position) { return playerList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Player myPlayer = (Player) getItem(position); Log.i(MainActivity.TAG,"inflattering the layout"); myInflatter = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View Row = myInflatter.inflate(R.layout.players_list, parent, false); TextView name = (TextView) Row.findViewById(R.id.nameHere); TextView id = (TextView) Row.findViewById(R.id.idHere); name.setText(myPlayer.getName()); name.setText(myPlayer.getId()); Log.i(MainActivity.TAG,"positioned name: "+myPlayer.getName()+". id: "+myPlayer.getId().toString()+"\n"); return Row; } } content_main.xml
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.sandeshpoudel.lab13.MainActivity" android:id="@+id/Url_input"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET XML" android:id="@+id/xml_button" android:layout_below="@+id/url_input" android:layout_centerHorizontal="true" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/url_input" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="http://users.metropolia.fi/~peterh/players.xml" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_player" android:layout_below="@+id/xml_button" /> players_list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.sandeshpoudel.lab13.MainActivity" android:id="@+id/Url_input" android:orientation="horizontal" android:background="#99ccff"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:inputType="number" android:textSize="20sp" android:id="@+id/idHere"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="4" android:inputType="textPersonName" android:textSize="20sp" android:id="@+id/nameHere"/> Error Log: 04-12 12:01:18.196 25131-25131/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sandeshpoudel.lab13, PID: 25131 java.lang.NullPointerException at com.example.sandeshpoudel.lab13.MainActivity.getList(MainActivity.java:45) at com.example.sandeshpoudel.lab13.Fetch_Xml.onPostExecute(Fetch_Xml.java:51) at com.example.sandeshpoudel.lab13.Fetch_Xml.onPostExecute(Fetch_Xml.java:20) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5694) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)
No comments:
Post a Comment