该应用程序运行,但运行不正常。字符串列表不会出现,但是我可以单击屏幕,它尝试显示桃子图像,但是只有其中的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(), 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>
我该如何解决?我什至不知道如何调试或任何东西。我需要教。
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