2016-04-12 4 views
-4

Ich habe versucht, ein Problem für ein paar Stunden jetzt zu lösen, aber ich habe es versäumt, es herauszufinden. Ich habe die Daten aus einer Online-XML-Datei über eine Async-Task abgerufen und den Basisadapter aufgerufen, um die Daten in einer Listenansicht zu füllen. Wenn ich den Adapter jedoch auf die Listenansicht einstelle, gibt es einen Null Pointer Exception-Fehler, und ich kann nicht herausfinden, welches Objekt tatsächlich Null ist. Jede Hilfe wäre willkommen, danke!Null Zeiger Ausnahme auf Set Adapter Android

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 Klasse

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"/> 

Fehlerprotokoll:

<!-- 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) 

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1107) bei dalvik.system.NativeStart.main (native Methode) ->

Fetch_Xml

package com.example.sandeshpoudel.lab13; 
import android.os.AsyncTask; 
import android.util.Log; 
import org.xmlpull.v1.XmlPullParserException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.nio.charset.MalformedInputException; 
import java.util.ArrayList; 
import java.util.List; 


public class Fetch_Xml extends AsyncTask<String,Void,List<Player>> { 
private static final String TAG ="my-filter"; 
List<Player> newPlayerList; 

@Override 
protected List<Player> doInBackground(String ... params) { 
    String url = params[0]; 
    InputStream inputStream = null; 
    newPlayerList = null; 
    try { 
     inputStream = convertUrl(url.toString()); 
     newPlayerList = new ArrayList<>(new Xml_parser().parse(inputStream)); 
    }catch (MalformedURLException i){ 
     Log.i(TAG,"Error: "+i.toString()); 

    }catch (IOException i){ 
     Log.i(TAG,"Error: "+i.toString()); 
    } catch (XmlPullParserException e) { 
     Log.i(TAG,"Error: "+e.toString()); 
    } 

    for(Player P:newPlayerList){ 
     Log.i(TAG,"______________________\n Player Name: "+P.getName()+"\t Id "+P.getId()+"\n"); 
    } 
    return newPlayerList; 

} 

@Override 
protected void onPostExecute(List<Player>players) { 

new MainActivity().getList(players); 


} 

private InputStream convertUrl(String s) throws MalformedURLException,IOException { 
    URL url = new URL(s); 
    HttpURLConnection myConnection = (HttpURLConnection) url.openConnection(); 
    myConnection.setRequestMethod("GET"); 
    myConnection.setReadTimeout(10000); 
    myConnection.setConnectTimeout(10000); 
    myConnection.setDoInput(true); 
    myConnection.connect(); 
    Log.i(TAG,"coverted url to Input Stream"); 
    return myConnection.getInputStream(); 
} 
} 
+4

poste das Fehlerprotokoll – SMR

+1

Sandesh wo rufst du deine getList Methode an ???? : o –

+0

private ListAdapter newAdapter; Dies deklarieren Sie global und welche Verwendung verwenden Sie newAdapter = new AdapterExample (this, players); –

Antwort

0

in der MainActivity Sie haben eine Methode public void getList(List<Player>players){

dort ist die Initialisierung des Objekts AdapterExample genannt ...

Sie nie sind diese Methode aufrufen ...

+0

aufrufen ich hatte diese Methode aufgerufen und Spieler ist nicht null auch ... –

+0

müssen Sie den Code relevanten Teile –

+0

danke für der Vorschlag jetzt habe ich den ganzen benötigten Code hinzugefügt ... –

0

Es scheint, dass Sie rufen die Methode getList nicht auf.

Dann könnten Sie List<Player> players = new ArrayList<>; Nullzeiger-Ausnahme zu vermeiden.

Ein Vorschlag, versuchen Sie, Ihren Code zu formatieren, ein gutes Format ist sehr wichtig.