-1

I am trying to make an app to list the schools in my area and I made a table in a database to contain these schools. I made a list view with clickable items to list the names of the schools and when I click on the item to move to another activity and send the clicked school name in order to list more information about the school, the app stops working. The following is one of the activities that contains this list view:

package com.example.welcome.madrasti;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ListOfSchoolsPublic extends AppCompatActivity {

        Intent redirect;
        Intent item;
        ListView list;
        ArrayList<String>  _sa;
        DBAdapter db;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_of_schools_public);

            openDB();

            list= (ListView) findViewById(R.id.usersList);

            redirect = getIntent();

            Cursor cursor = db.getAllRows();

            _sa= new  ArrayList<String>();

            //cursor.moveToFirst();

            Log.d("CURSORCOUNT","Number of rows in the Cursor is = " + String.valueOf(cursor.getCount()));




            while(cursor.moveToNext()) {

                if(cursor.getString(DBAdapter.COL_SChOOL_TYPE_TABLE_2).equals("حكومية"))
                _sa.add(cursor.getString(DBAdapter.COL_SCHOOLNAME_TABLE_2));

            }

            cursor.close();


            @SuppressWarnings("rawtypes")
            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,_sa);

            list.setAdapter(new TempLyaout(ListOfSchoolsPublic.this,_sa));


           list.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
                {

                    String selectedItem = (String) arg0.getItemAtPosition(position);

                    itemInfo(selectedItem);
                }
            });



    }

    ///////////////////////////////////////////////////////////////

    public void itemInfo(String selectedItem){

           item = new Intent(getApplicationContext(), NewSchool.class);

           item.putExtra("schoolName",selectedItem);

           startActivity(item);

    }

    ///////////////////////////////////////////////////////////////
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDB();

    }// end onDestroy method



    ///////////////////////////////////////////////////////////////

    private void openDB() {
        db = new DBAdapter(ListOfSchoolsPublic.this);
        db.open();
    } // end openDB method

    //////////////////////////////////////////////////////////////

    private void closeDB() {
        db.close();
    } // end closeDB method

}

The following is the activity that each list item should move to:

package com.example.welcome.madrasti;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NewSchool extends AppCompatActivity {

    Intent intent;

    Intent nextPage;

    Button next;

    DBAdapter db;

    TextView t1;

    TextView t2;

    TextView t3;

    TextView t4;

    TextView t5;



    String name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_school);

        t1=(TextView)findViewById(R.id.textView9) ;
        t2=(TextView)findViewById(R.id.textView11) ;
        t3=(TextView)findViewById(R.id.textView12) ;
        t4=(TextView)findViewById(R.id.textView17) ;
        t5=(TextView)findViewById(R.id.textView18) ;


        next = (Button) findViewById(R.id.button);
        intent = getIntent();

        name = intent.getStringExtra("schoolName");

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goNext();
            }
        });

        Cursor cursor = db.getAllRows();

        while(cursor.moveToNext()) {

            if(cursor.getString(DBAdapter.COL_SCHOOLNAME_TABLE_2).equals(name)){

                t1.setText(DBAdapter.COL_SCHOOLNAME_TABLE_2);
                t2.setText(DBAdapter.COL_FOUNDATION_DATE_TABLE_2);
                t3.setText(DBAdapter.COL_BUY_BOOKS_TABLE_2);
                t4.setText(DBAdapter.COL_HEALTH_TABLE_2);
                t5.setText(DBAdapter.COL_LOCATION_TABLE_2);

            }


        }

        cursor.close();


    }


    public void goNext(){

        nextPage = new Intent(getApplicationContext(),NewShoolAct2.class);
        nextPage.putExtra("schoolName",name);
        startActivity(nextPage);

    }

    ///////////////////////////////////////////////////////////////
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDB();

    }// end onDestroy method



    ///////////////////////////////////////////////////////////////

    private void openDB() {
        db = new DBAdapter(NewSchool.this);
        db.open();
    } // end openDB method

    //////////////////////////////////////////////////////////////

    private void closeDB() {
        db.close();
    } // end closeDB method


}

The following is the XML of the previous activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_background"
    tools:context="com.example.welcome.madrasti.NewSchool">

    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="0dp">


        <TextView
            android:id="@+id/textView"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_x="244dp"
            android:layout_y="54dp"
            android:text="إسم المدرسة:"
            android:textAlignment="center"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="dp"
            tools:layout_editor_absoluteY="10dp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="240dp"
            android:layout_y="127dp"
            android:text="تاريخ التأسيس:"
            android:textAlignment="center"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="297dp"
            tools:layout_editor_absoluteY="144dp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="231dp"
            android:layout_y="185dp"
            android:text="كيفية شراء الكتب:"
            android:textAlignment="center"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="284dp"
            tools:layout_editor_absoluteY="210dp" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="215dp"
            android:layout_y="265dp"
            android:text="وضع المرافق الصحية:"
            android:textAlignment="center"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="265dp"
            tools:layout_editor_absoluteY="288dp" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="244dp"
            android:layout_y="340dp"
            android:text="موقع المدرسة:"
            android:textAlignment="center"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="300dp"
            tools:layout_editor_absoluteY="368dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="98dp"
            android:layout_height="34dp"
            android:layout_x="133dp"
            android:layout_y="434dp"
            android:background="@drawable/button"
            android:text="التالي"
            android:textAlignment="center"
            android:textColor="#ffff"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="45dp"
            tools:layout_editor_absoluteY="426dp" />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="11dp"
            android:layout_y="77dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="18dp"
            tools:layout_editor_absoluteY="59dp" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="12dp"
            android:layout_y="148dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="12dp"
            tools:layout_editor_absoluteY="150dp" />

        <TextView
            android:id="@+id/textView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="32345dp"
            android:layout_y="32418dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="16dp"
            tools:layout_editor_absoluteY="235dp" />

        <TextView
            android:id="@+id/textView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="15dp"
            android:layout_y="299dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="21dp"
            tools:layout_editor_absoluteY="37dp" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="16dp"
            android:layout_y="372dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="17dp"
            tools:layout_editor_absoluteY="381dp" />

        <TextView
            android:id="@+id/textView20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="16dp"
            android:layout_y="219dp"
            android:text="TextView"
            tools:layout_editor_absoluteX="10dp"
            tools:layout_editor_absoluteY="215dp" />
    </AbsoluteLayout>

</android.support.constraint.ConstraintLayout>

The following is the log cat when the app is crashed:

04-13 17:57:49.245 5828-5828/? E/libprocessgroup: failed to make and chown /acct/uid_10059: Read-only file system
04-13 17:57:49.245 5828-5828/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
04-13 17:57:49.245 5828-5828/? I/art: Late-enabling -Xcheck:jni
04-13 17:57:49.435 5828-5838/? I/art: Debugger is no longer active
04-13 17:57:49.492 5828-5828/? I/InstantRun: starting instant run server: is main process
04-13 17:57:49.602 5828-5843/? W/art: Suspending all threads took: 11.737ms
04-13 17:57:49.605 5828-5843/? I/art: Background sticky concurrent mark sweep GC freed 2038(168KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 832KB/1117KB, paused 13.068ms total 21.305ms
04-13 17:57:49.630 5828-5828/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-13 17:57:49.779 5828-5847/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-13 17:57:49.784 5828-5828/? D/Atlas: Validating map...
04-13 17:57:49.810 5828-5847/? D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
    loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-13 17:57:49.820 5828-5847/? D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
04-13 17:57:49.848 5828-5847/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-13 17:57:49.878 5828-5847/? D/OpenGLRenderer: Enabling debug mode 0
04-13 17:57:49.896 5828-5847/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-13 17:57:49.896 5828-5847/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2c15340, error=EGL_SUCCESS

What is the problem please? how to fix it?

  • Those logs aren't what you need. You're looking for [the stack trace from the crash](http://stackoverflow.com/questions/23353173), which is likely all in red. – Mike M. Apr 13 '18 at 22:26
  • Look into your LogCat as Mike says but look for the messages with red color and that starts with the text "Caused by". – Jorgesys Apr 13 '18 at 22:50

4 Answers4

0

You can Store your string into some other class like this:

Store string:

String selectedItem = (String) arg0.getItemAtPosition(position);

Share.love=selectedItem ;

Get string:

in Your next Activity

Textview textview=(Textview)findviewbyid(R.id.yourname);
textview.setText(Share.love);

Make class

public class Share {

public static String love = "";

}
Mayank Garg
  • 1,284
  • 1
  • 11
  • 23
0

Use intents and read basic android first. There are number of options already available for this.

  • 1
    True. However, this does not explain why the app crashes (last sentence of the question), which might be what Student Engineering wanted to know –  Apr 14 '18 at 07:28
0

Try the following example (It uses putExtra() to pass a string to another activity):

Demo15.class:------------

public class Demo15 extends AppCompatActivity {

private Button b;
private EditText edt;
private final String EXTRA_TEXT_ = "extra_text_";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo17);

    edt = (EditText) findViewById(R.id.edt);

    b = (Button) findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Demo15.this, Demo16.class);
            if (!edt.getText().toString().isEmpty()) {
                i.putExtra(EXTRA_TEXT_, edt.getText().toString());
            }
            startActivity(i);
        }
    });
}
}

Demo16.class:-------

public class Demo16 extends AppCompatActivity {

private TextView tv;
private final String EXTRA_TEXT_ = "extra_text_";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo18);

    tv = (TextView) findViewById(R.id.tv);

    if (getIntent() != null) {
        if (getIntent().getStringExtra(EXTRA_TEXT_) != null) {
            tv.setText(getIntent().getStringExtra(EXTRA_TEXT_));
        }
    }
}
}

demo17.xml:-------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter text to pass"
    android:id="@+id/edt"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Start the other activity"
    android:textAllCaps="false"
    android:layout_marginTop="50dp"
    android:id="@+id/b"/>

</LinearLayout>

demo18.xml:---------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="####"
    android:id="@+id/tv"/>

</android.support.constraint.ConstraintLayout>
Brainnovo
  • 1,749
  • 2
  • 12
  • 17
0

Use bundle with intents as android standards. Example:

Intent intent=new Intent(CurrentActivity.this,NewActivity.class);
intent.putExtra("schoolname","Name of school");
startActivity
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39