作者:星空下的舞者j | 来源:互联网 | 2023-05-19 11:42
该应用程序运行,但运行不正常。字符串列表不会出现,但是我可以单击屏幕,它尝试显示桃子图像,但是只有其中的1/2 ...而其他三个则不会出现。
E / StudioProfiler:JVMTI错误: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(),options);
img.setImageBitmap(scaledImg);
}
}
strings.xml
List App
- peaches
- tomato
- squash
- $0.99
- $1.49
- $0.89
- Fresh peaches from Georgia
- Fresh salad tomatoes from Ohio
- Fresh squash from California
我该如何解决?我什至不知道如何调试或任何东西。我需要教。
E / StudioProfiler:JVMTI错误: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