0

I'm new to android and I'm doing a project in which I have a grid view with Image and text. When I press any particular image, I want it to go to another page and retrieve data from database and display it in that screen.

My php code works fine. But there is some problem with the java code.If I run it, I got unfortunately DemoGridView(my app name) stopped.

This is my code:

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>

I'm really new to android and I'm not sure what is the problem with this code. Thank you all in advance for your help.

This is what I got in logcat

09:46:29 NullPointerException: Error executing task com.android.tools.idea.uibuilder.editor.NlPreviewForm$$Lambda$193/557090381@475c7540
swa95
  • 23
  • 5

0 Answers0