4

The app runs but not as it should. The list of strings will not appear, but I can click the screen and it tries to show the peaches image, but only 1/2 of it... and the other three do not come to appear.

E/StudioProfiler: JVMTI error: 103

MainActivity.java

package org.ibg.brad.listapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


    ListView myListView;
    String[] items;
    String[] price;
    String[] descriptions;



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

        Resources res = getResources();
        myListView = (ListView) findViewById(R.id.myListView);
        items = res.getStringArray(R.array.items);
        price = res.getStringArray(R.array.prices);
        descriptions = res.getStringArray(R.array.descriptions);

        ItemAdapter itemAdapter = new ItemAdapter(this, items, price, descriptions);
        myListView.setAdapter(itemAdapter);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent showDetailActivity = new Intent(getApplicationContext(), DetailActivity.class);
                showDetailActivity.putExtra("org.ibg.brad.listapp.ITEM_INDEX", position);
                startActivity(showDetailActivity);
            }
        });
    }
}

ItemAdapter.java

package org.ibg.brad.listapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


/**
 * Created by brad on 12/28/19.
 */

public class ItemAdapter extends BaseAdapter {


    LayoutInflater mInflater;
    String[] items;
    String[] price;
    String[] descriptions;

    public ItemAdapter(Context c, String[] i, String[] p, String[] d) {

        items = i;
        price = p;
        descriptions = d;
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {return items.length;
    }
            //Here 'int positions' below are auto generated in implementing abstract methods.
    @Override
    public Object getItem(int position) {return items[position];
    }

    @Override
    public long getItemId(int position) {return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = mInflater.inflate(R.layout.my_listview_detail, null);
        TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
        TextView descriptionTextView = (TextView) v.findViewById(R.id.descriptionTextView);
        TextView priceTextView = (TextView) v.findViewById(R.id.priceTextView);

        String name = items[position];
        String desc = descriptions[position];
        String cost = price[position];

        nameTextView.setText(name);
        descriptionTextView.setText(desc);
        priceTextView.setText(cost);

        return v;
    }
}

DetailActivity.java

package org.ibg.brad.listapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;

public class DetailActivity extends AppCompatActivity {

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

        Intent in = getIntent();
        int index = in.getIntExtra("org.ibg.brad.listapp.ITEM_INDEX", -1);

        if (index > -1) {
            int pic = getImg(index);
            ImageView img = (ImageView) findViewById(R.id.imageView);
            scaleImg(img, pic);
        }

    }

    private int getImg(int index) {
        switch (index) {
            case 0: return R.drawable.peaches;
            case 1: return R.drawable.tomato;
            case 2: return R.drawable.squash;
            default: return -1;
        }
    }

    private void scaleImg(ImageView img, int pic) {

        Display screen = getWindowManager().getDefaultDisplay();
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), pic, options);

        int imgWidth = options.outWidth;
        int screenWidth = screen.getWidth();

        if (imgWidth > screenWidth) {
            int ratio = Math.round( (float)imgWidth / (float)screenWidth );
            options.inSampleSize = ratio;
        }

        options.inJustDecodeBounds = false;
        Bitmap scaledImg = BitmapFactory.decodeResource(getResources(), pic, options);
        img.setImageBitmap(scaledImg);

    }

}

strings.xml


<resources>
    <string name="app_name">List App</string>

    <string-array name="items">
        <item>peaches</item>
        <item>tomato</item>
        <item>squash</item>
    </string-array>

    <string-array name="prices">
        <item>$0.99</item>
        <item>$1.49</item>
        <item>$0.89</item>
    </string-array>

    <string-array name="descriptions">
        <item>Fresh peaches from Georgia</item>
        <item>Fresh salad tomatoes from Ohio</item>
        <item>Fresh squash from California</item>
    </string-array>

</resources>

How do I resolve this? I do not even know how to debug or anything. I need to be taught.

E/StudioProfiler: JVMTI error: 103(JVMTI_ERROR_ILLEGAL_ARGUMENT)

V/StudioProfiler: Acquiring Application for Events W/InputMethodManager: InputMethodManager.getInstance() is deprecated because it cannot be compatible with multi-display. Use context.getSystemService(InputMethodManager.class) instead. java.lang.Throwable at android.view.inputmethod.InputMethodManager.getInstance(InputMethodManager.java:987) at java.lang.reflect.Method.invoke(Native Method) at com.android.tools.profiler.support.profilers.EventProfiler$InputConnectionHandler.run(EventProfiler.java:262) at java.lang.Thread.run(Thread.java:919) W/InputMethodManager: InputMethodManager.peekInstance() is deprecated because it cannot be compatible with multi-display. Use context.getSystemService(InputMethodManager.class) instead. java.lang.Throwable at android.view.inputmethod.InputMethodManager.peekInstance(InputMethodManager.java:1006) at android.view.inputmethod.InputMethodManager.getInstance(InputMethodManager.java:992) at java.lang.reflect.Method.invoke(Native Method) at com.android.tools.profiler.support.profilers.EventProfiler$InputConnectionHandler.run(EventProfiler.java:262) at java.lang.Thread.run(Thread.java:919) V/StudioProfiler: Live memory tracking disabled. New JNI table set.


[ListApp not working][1]
[Android Studio build/emulator][2]


  [1]: https://i.stack.imgur.com/sbzEN.png
  [2]: https://i.stack.imgur.com/mkE1q.jpg
  • Can you send me the code? So i can fit the issue and send you back the code. Secondly ListView is not recommended to use anymore, go for the recyclerView. – Daniyal Nasir Dec 29 '19 at 13:16
  • Thanks for the feedback. I will probably just start over. –  Dec 29 '19 at 16:58
  • In what way would you want me to send this code to you? –  Dec 29 '19 at 17:18
  • You can upload the code on github, dropbox or googleDrive. Its up to you. – Daniyal Nasir Dec 30 '19 at 07:22
  • Thanks, I will keep this in mind in the future should we need to go down that road. In following up I just wanted to let you know I added it to my git-hub. https://github.com/HarrisMBrad/List-App –  Jan 04 '20 at 15:29
  • I have checked your code and I think, I have got the right result. I have made a few images of the result. Check the images and let me know if there is any issue. http://www.mediafire.com/folder/knz7llope37i6/Images – Daniyal Nasir Jan 06 '20 at 13:22
  • 1
    @DaniyalNasir: Your way of answering questions doesn't help anyone at all. Would you please post your code here? Thx! – Fred Jan 18 '21 at 01:08

0 Answers0