我是android的新手,我正在做一个项目,其中我有一个带有图像和文本的网格视图。当我按下任何特定图像时,我希望它转到另一个页面并从数据库中检索数据并将其显示在该屏幕中。
我的PHP代码工作正常。但是java代码存在一些问题。如果我运行它,很遗憾DemoGridView(我的应用程序名称)停止了。
这是我的代码:
GridViewImageTextActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_image_text_example);
CustomGridViewActivity adapterViewAndroid = new CustomGridViewActivity(GridViewImageTextActivity.this, gridViewString, gridViewImageId);
androidGridView=(GridView)findViewById(R.id.grid_view_image_text);
androidGridView.setAdapter(adapterViewAndroid);
androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int i, long id) {
Intent s = new Intent(GridViewImageTextActivity.this, DisplayStateWise.class);
s.putExtra("stateValue",gridViewString[+i]);
startActivity(s);
}
});
Config.java
public class Config {
public static final String DATA_URL = "http://demoprojects.000webhostapp.com/getData.php?State=";
public static final String KEY_NAME = "Name";
public static final String KEY_STATE = "State";
public static final String KEY_C = "Catagory";
public static final String JSON_ARRAY = "result";
}
DisplayStateWise.java
public class DisplayStateWise extends AppCompatActivity {
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_states);
String data = getIntent().getExtras().getString("stateValue");
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+data;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(DisplayStateWise.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String Name="";
String State="";
String Catagory = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
Name = collegeData.getString(Config.KEY_NAME);
State = collegeData.getString(Config.KEY_STATE);
Catagory = collegeData.getString(Config.KEY_C);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+Name+"\nAddress:\t" +State+ "\nVice Chancellor:\t"+ Catagory);
}
}
display_states.xml
<?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: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"
tools:context=".DisplayStateWise" >
<ListView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp" >
</ListView>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.swathi.demogridview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".GridViewImageTextActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DisplayStateWise"/>
</application>
</manifest>
我对Android很新,我不确定这段代码有什么问题。提前感谢大家的帮助。
这是我在logcat中得到的
09:46:29 NullPointerException: Error executing task com.android.tools.idea.uibuilder.editor.NlPreviewForm$$Lambda$193/557090381@475c7540