2017-05-12 6 views
0

Ich habe ein Beispiel online verfolgt, um JSON-Inhalte über einen RecyclerView herunterzuladen, zu analysieren und anzuzeigen. Avaialble: https://www.youtube.com/watch?v=SRmjXr6Ui-IAnwendung stürzt ab, wenn mit RecyclerView

Wenn ich die App auf dem Emulator ausführen, erhalte ich einen Fehlercode. Kann es nicht herausfinden, und ich habe andere Threads auf dieser Website mit ähnlichen Problemen durchsucht, aber ich war nicht in der Lage, mein Problem zu lösen.

Der Fehlercode Ich erhalte ist:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 

Der Fehler auf der Leitung auftritt 62 der JSONparzer Klasse.

Das ist rv.setAdapter (neues MyAdapter (c, Benutzer));

public class MainActivity extends AppCompatActivity { 

    String jsonURL = "http://jsonplaceholder.typicode.com/users"; 

    public static final String TAG = "MainActivity"; 
    //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 

    private RecyclerView rv; 
    /*private ArrayList<Standing> mLeagueTable = new ArrayList<>(); //Initalise m 
    private TableAdapter mTableAdapter = new TableAdapter(mLeagueTable, this); */ 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     //setSupportActionBar(toolbar); 

     rv = (RecyclerView) findViewById(R.id.rv); 
     if (rv != null) { 
      rv.setLayoutManager(new LinearLayoutManager(this)); 
     } 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); //Initialze FAB 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new JSONDownloader(MainActivity.this, jsonURL, rv).execute(); 
      } 
     }); 
    } 
} 



class JSONParzer extends AsyncTask<Void, Void, Boolean> { 

    private Context c; 
    private String jsonData; 
    private RecyclerView rv; 

    private ProgressDialog pd ; 
    private ArrayList<User> users = new ArrayList<>(); 

    public JSONParzer(Context c, String jsonData, RecyclerView rv) { 
     this.c = c; 
     this.jsonData = jsonData; 
     this.rv = rv; 
    } 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pd = new ProgressDialog(c); 
     pd.setTitle("Parse"); 
     pd.setMessage("Parsing..Please Wait"); 
     pd.show(); 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 

     return parse(); 
    } 

    @Override 
    protected void onPostExecute(Boolean isParsed) { 
     super.onPostExecute(isParsed); 

     pd.dismiss(); //Dismiss progress dialog. 
     if (isParsed) 
     { 
      //BIND(using the adapters) 
      rv.setAdapter(new MyAdapter(c, users)); //Pass in instance of adapter. 

     } else 
     { 
      Toast.makeText(c, "Unable to Parse check your Log Output", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private Boolean parse() 
    { 
     try { 
      JSONArray ja = new JSONArray(jsonData); 
      JSONObject jo; //declare json OBJECT 

      users.clear(); 
      User user; 

      for (int i=0; i< ja.length(); i++) //iterating in JSONArray 
      { 
       jo = ja.getJSONObject(i); 
       String name = jo.getString("name"); 
       String username = jo.getString("username"); 
       String email = jo.getString("email"); 

       user = new User();  //Create a new "User" object. 

       user.setName(name); 
       user.setUsername(username); 
       user.setEmail(email); 

       users.add(user); //Add new user object to ArrayList users. 
      } 
      return true; 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 


} 

Der Fehler tritt in Zeile 62 der JSONParzer-Klasse auf.

Ich werde die Layout-Dateien auch darunter einfügen.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="16dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    tools:context=".UI.MainActivity" 
    tools:showIn="@layout/activity_main" > 
content_main 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </android.support.v7.widget.RecyclerView> 


</RelativeLayout> 

activity_main

<?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:id="@+id/activity_main" 
    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" 
    android:orientation="vertical" 
    tools:context="com.example.oisin.premierleaguesocial.UI.MainActivity" 
    android:weightSum="1"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/app_name" 
     android:id="@+id/test" 
     android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignEnd="@+id/test" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/test" 
     android:clickable="true" 
     app:fabSize="mini" 
     app:srcCompat="@android:drawable/presence_online" /> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:background="?attr/colorPrimary" 
     android:contentInsetStart="0dp" 
     android:contentInsetLeft="0dp" 
     app:contentInsetLeft="0dp" 
     app:contentInsetStart="0dp" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 


</RelativeLayout> 

Jede Hilfe wäre sehr dankbar. Vielen Dank für Ihre Zeit.

Antwort

0

Der Absturz ist passiert, weil das Android-System die Recycler-Ansicht von "activity_main.xml" nicht finden konnte.

Bitte versuchen Sie die follwing Code "activity_main.xml" Layout hinzufügen:

<android.support.v7.widget.RecyclerView 
    android:id="@+id/rv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</android.support.v7.widget.RecyclerView> 

werden, so würde das geänderte Layout so etwas wie:

<?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:id="@+id/activity_main" 
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" 
android:orientation="vertical" 
android:weightSum="1"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    android:id="@+id/test" 
    android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignEnd="@+id/test" 
    android:layout_alignParentBottom="true" 
    android:layout_alignRight="@+id/test" 
    android:clickable="true" 
    app:fabSize="mini" 
    app:srcCompat="@android:drawable/presence_online" /> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:background="?attr/colorPrimary" 
    android:contentInsetStart="0dp" 
    android:contentInsetLeft="0dp" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

<android.support.v7.widget.RecyclerView 
    android:layout_below="@+id/toolbar" 
    android:id="@+id/rv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</android.support.v7.widget.RecyclerView> 


</RelativeLayout> 
+0

Vielen Dank für Ihre Hilfe. Dies hat das Problem gelöst. Ich habe diesen Code in activity_main eingefügt und musste ihn in content_main haben. Jetzt habe ich es aus content_main herausgenommen und es jetzt nur noch in activity_main. Funktioniert perfekt. – Oisin834

+0

Gern geschehen. Wenn es Ihr Problem gelöst hat, markieren Sie dies als Lösung, damit es anderen hilft, die ähnliche Probleme haben. –

0

Es gibt keine RecyclerView in Ihrem activity_main XML ist und diese verursacht NullPointerException.

Fügen Sie RecyclerView zu activity_main hinzu und versuchen Sie es erneut.

<?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:id="@+id/activity_main" 
    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" 
    android:orientation="vertical" 
    tools:context="com.example.oisin.premierleaguesocial.UI.MainActivity" 
    android:weightSum="1"> 

    ................ 
    ......................... 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </android.support.v7.widget.RecyclerView> 

</RelativeLayout> 
Verwandte Themen