我正在尝试在我的模拟器/设备上运行Android App。
没有编译时错误,但在设备/模拟器和
上运行时应用程序崩溃"不幸的是,GridViewMapExample已停止"消息ID显示在屏幕上。
MainActivity.java
<local:MyControl
// properties
/>
的strings.xml
package com.example.gridviewmapexample;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView) findViewById(R.id.gridView);// step 3
grid.setAdapter(new NikAdapter(this));
}
}
class Country {
String countryName;
int imageId;
Country(String countryName, int imageId) {
this.countryName = countryName;
this.imageId = imageId;
}
}
class NikAdapter extends BaseAdapter {
ArrayList<Country> list;
Context c;
NikAdapter(Context c) {
this.c = c;
list = new ArrayList<Country>();
Resources res = c.getResources();
String[] tempCountryNames = res.getStringArray(R.array.country_names);
int[] countryImages = { R.drawable.germany_flag, R.drawable.india_flag,
R.drawable.italy_flag, R.drawable.norway_flag,
R.drawable.pakistan_flag, R.drawable.saudi_arabia_flag };
for (int i = 0; i < 6; i++) {
Country tempCountry = new Country(tempCountryNames[i],
countryImages[i]);
list.add(tempCountry);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
ImageView myImageView;
ViewHolder(View v) {
myImageView = (ImageView) v.findViewById(R.id.imageView);
}
}
@Override
public View getView(int i, View view, ViewGroup parent) {
View row = view;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAUNCHER_APPS_SERVICE);
row = inflater.inflate(R.layout.single_item, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Country temp = list.get(i);
holder.myImageView.setImageResource(temp.imageId);
holder.myImageView.setTag(temp);
return row;
}
}
single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GridViewMapExample</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="country_names">
<item>Germany</item>
<item>India</item>
<item>Italy</item>
<item>Norway</item>
<item>Pakistan</item>
<item>Saudi Arabia </item>
</string-array>
</resources>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/india_flag" />
</RelativeLayout>
我得到了以下堆栈跟踪,现在搜索了很长时间后,我仍然不知道造成它的原因。
<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"
tools:context="com.example.gridviewmapexample.MainActivity" >
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:columnWidth="120dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
答案 0 :(得分:0)
现在您已经发布了堆栈跟踪(顺便说一下,不需要其余日志),您应该能够找到问题发生的位置。
堆栈跟踪告诉您NPE(NullPointerException
)出现在getView()
的第98行的MainActivity.java
。
我不知道98行是哪一行,但我发现getView()
存在潜在问题:
LayoutInflater
,但请致电getSystemService(Context.LAUNCHER_APPS_SERVICE)
。您可能想要致电getSystemService(Context.LAYOUT_INFLATER_SERVICE)
。