我正在嘗試爲拉力賽開發一個Android應用程序。 我ActivityClass:在ListView中解析Json
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
data_list = new ArrayList<>();
load_data();
gridLayoutManager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
}
public void load_data()
{
String username = getIntent().getStringExtra("username");
String password = getIntent().getStringExtra("password");
try
{
restApi=new RallyRestApi(new URI("https://rally1.rallydev.com"),username,password);
QueryRequest userstory = new QueryRequest("HierarchicalRequirement");
userstory.setFetch(new Fetch((new String[] {"Name", "FormattedID","Tasks"})));
QueryResponse responseus = restApi.query(userstory);
JsonArray userstory1 = restApi.query(userstory).getResults();
for(int i=0;i<responseus.getResults().size();i++)
{
JsonObject storyJsonObject = responseus.getResults().get(i).getAsJsonObject();
QueryRequest taskRequest = new QueryRequest(storyJsonObject.getAsJsonObject("Tasks"));
taskRequest.setFetch(new Fetch("Name","Actuals","Estimate"));
JsonArray tasks = restApi.query(taskRequest).getResults();
for(int j=0;j<tasks.size();j++)
{
MyData data = new MyData(userstory1.get(i).getAsJsonObject().get("Name").toString(),tasks.get(j).getAsJsonObject().get("Name").toString(),tasks.get(j).getAsJsonObject().get("Actuals").toString(),tasks.get(j).getAsJsonObject().get("Estimates").toString());
System.out.println("User Story : "+storyJsonObject.get("Name")+" Task :"+tasks.get(j).getAsJsonObject().get("Name")+" Actuals :" +tasks.get(j).getAsJsonObject().get("Actuals")+" Estimate :"+tasks.get(j).getAsJsonObject().get("Estimates"));
data_list.add(data);
}
}
}
catch (URISyntaxException | IOException e)
{
e.printStackTrace();
}
}
AdapterClass:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private List<MyData> my_data;
public CustomAdapter(Context context, List<MyData> my_data) {
this.context = context;
this.my_data = my_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.userstory.setText(my_data.get(position).getUserstory());
holder.tasks.setText(my_data.get(position).getTask());
holder.actual.setText(my_data.get(position).getActuals());
holder.actual.setText(my_data.get(position).getActuals());
}
@Override
public int getItemCount() {
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView userstory,tasks,actual,estimate;
public ViewHolder(View itemView) {
super(itemView);
userstory=(TextView) itemView.findViewById(R.id.tvUserStory);
tasks=(TextView) itemView.findViewById(R.id.tvTask);
actual=(TextView) itemView.findViewById(R.id.tvActual);
estimate=(TextView) itemView.findViewById(R.id.tvEstimate);
}
}
}
getter/setter方法:
public class MyData
{
private String userstory;
private String task;
private String actuals;
private String todos;
//getters/setters
錯誤日誌:
了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.bmc.apetkar.akshay_rallyrest/com.bmc.apetkar.akshay_rallyrest.FirstPage}: 顯示java.lang.NullPointerException:嘗試調用虛擬方法 「java.lang.String中com.google.gson.JsonElement.toString( )」上的空 對象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread。 -wrap11(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) at androi d.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java。 lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit。 java:616) 引起:java.lang.NullPointerException:試圖調用虛擬 方法的'java.lang.String com.google.gson.JsonElement.toString()' 空對象引用 在 com.bmc.apetkar.akshay_rallyrest.FirstPage.load_data(FirstPage.java:72) 在 com.bmc.apetkar.akshay_rallyrest.FirstPage.onCreate(FirstPage.java:45) 在android.app.Activity.performCreate( Activity.java:6237)
我得到在活動課load_data method.The數據NPE在運行time.What加載也可能會被用於this.I需要知道,如果有什麼不對的原因我正在設置數據到視圖或某些數據issue.Thanks
問題尋求幫助調試(「爲什麼不是這個代碼的工作?」)必須包括所期望的行爲,_a具體問題或錯誤_和_最短代碼必須重現**在問題本身**。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
我應該發佈錯誤日誌嗎? – Akshay
您發佈了大量代碼,但沒有顯示您想要幫助的錯誤的錯誤日誌。應該讓你覺得,不是嗎? – Ridcully