私はRecyclerViewを使用してJSONコンテンツをダウンロード、解析、表示するためのオンラインの例に従っています。私がエミュレータでアプリケーションを実行すると、エラーコードが表示されます。それを理解できないようで、私はこのウェブサイト上の他のスレッドを同様の問題で閲覧しましたが、問題を解決できませんでした。RecyclerViewでアプリケーションがクラッシュする
私は取得していますエラーコードは次のとおりです。
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
エラーがJSONparzerクラスのライン62で発生しています。
これはrv.setAdapter(new MyAdapter(c、users))です。
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;
}
}
}
JSONParzerクラスの62行目でエラーが発生しています。
以下のレイアウトファイルも含めます。
<?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>
activity_main
<?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に追加し、content_mainにそれを持っていなければなりません。だから私はcontent_mainから取り出し、今はactivity_mainだけでそれを取り出しました。作品は完璧です。 – Oisin834
ようこそ。それがあなたの問題を解決した場合は、同様の問題を抱えている他の人に役立つように、解決策としてマークしてください。 –