我一直在线跟踪一个示例,通过RecyclerView下载,解析和显示JSON内容。 Avaialble at:https://www.youtube.com/watch?v=SRmjXr6Ui-I
当我在模拟器上运行应用程序时,我收到错误代码。似乎无法弄清楚,我已经浏览了这个网站上的其他线程,但是我还没有能够解决我的问题。
我得到的错误代码是:
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: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>
非常感谢任何帮助。谢谢你的时间。
答案 0 :(得分:0)
发生崩溃是因为android系统无法从“activity_main.xml”中找到回收站视图。
请尝试添加以下代码“activity_main.xml”布局:
curl -X POST -H "Jenkins-Crumb:${JENKINS_CRUMB}" http://${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/abort
因此改变后的布局将是:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
答案 1 :(得分:0)
RecyclerView
XML中没有activity_main
,导致NullPointerException
。
将RecyclerView
添加到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">
................
.........................
<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>