我有一个项目在Eclipse中的模拟器中工作正常,但在我的实际Nexus 7上没有。它应该是一个基本的电话簿应用程序,但到目前为止它只显示它保存在文本文件中的名称。当我在Nexus上运行它并点击“添加”按钮时,它会崩溃。它显然保存了数据,因为如果我再次打开它,数据就在那里。我试过寻找类似的问题,但我找不到任何问题。这是Logcat:
05-10 08:58:08.502: D/dalvikvm(16460): Late-enabling CheckJNI
05-10 08:58:08.532: D/dalvikvm(16460): Debugger has detached; object registry had 1 entries
05-10 08:58:08.682: D/libEGL(16460): loaded /system/lib/egl/libEGL_tegra.so
05-10 08:58:08.702: D/libEGL(16460): loaded /system/lib/egl/libGLESv1_CM_tegra.so
05-10 08:58:08.722: D/libEGL(16460): loaded /system/lib/egl/libGLESv2_tegra.so
05-10 08:58:08.752: D/OpenGLRenderer(16460): Enabling debug mode 0
05-10 08:58:26.612: D/dalvikvm(16460): GC_CONCURRENT freed 212K, 5% free 7454K/7812K, paused 10ms+2ms, total 45ms
05-10 08:58:33.752: D/AndroidRuntime(16460): Shutting down VM
05-10 08:58:33.752: W/dalvikvm(16460): threadid=1: thread exiting with uncaught exception (group=0x40d44930)
05-10 08:58:33.762: E/AndroidRuntime(16460): FATAL EXCEPTION: main
05-10 08:58:33.762: E/AndroidRuntime(16460): java.lang.NullPointerException
05-10 08:58:33.762: E/AndroidRuntime(16460): at edu.mansfield.wardba21.phonebook.MainActivity$StableArrayAdapter.getItemId(MainActivity.java:162)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.AbsListView.obtainView(AbsListView.java:2180)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.ListView.onMeasure(ListView.java:1158)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.View.measure(View.java:15518)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.View.measure(View.java:15518)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.View.measure(View.java:15518)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.View.measure(View.java:15518)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-10 08:58:33.762: E/AndroidRuntime(16460): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.View.measure(View.java:15518)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.Choreographer.doFrame(Choreographer.java:532)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.os.Handler.handleCallback(Handler.java:725)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.os.Looper.loop(Looper.java:137)
05-10 08:58:33.762: E/AndroidRuntime(16460): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-10 08:58:33.762: E/AndroidRuntime(16460): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 08:58:33.762: E/AndroidRuntime(16460): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 08:58:33.762: E/AndroidRuntime(16460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 08:58:33.762: E/AndroidRuntime(16460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-10 08:58:33.762: E/AndroidRuntime(16460): at dalvik.system.NativeStart.main(Native Method)
从那以后,我觉得它看起来与布局有关,但我不知道如何修复它。这是.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="41dp"
tools:listitem="@android:layout/simple_list_item_2" >
</ListView>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_marginLeft="69dp"
android:layout_toRightOf="@+id/editText1"
android:ems="10"
android:hint="Number"
android:inputType="phone" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="49dp"
android:layout_marginTop="21dp"
android:text="Add"
android:textSize="36sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/editText2"
android:layout_marginLeft="39dp"
android:text="Delete"
android:textSize="36sp" />
</RelativeLayout>
这是java:
package edu.mansfield.wardba21.phonebook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBtn=(Button)findViewById(R.id.button1);
Button delBtn=(Button)findViewById(R.id.button2);
final ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayList<String> names = new ArrayList<String>();
final ArrayList<String> numbers = new ArrayList<String>();
final EditText nameEdit=(EditText) findViewById(R.id.editText1);
final EditText numbEdit=(EditText) findViewById(R.id.editText2);
final File nameFile = new File(getFilesDir() + File.separator + "names.txt");
final File numbFile = new File(getFilesDir() + File.separator + "numbers.txt");
if(!nameFile.exists())
{
try {
nameFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(!numbFile.exists())
{
try {
numbFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try
{
Scanner sc = new Scanner(nameFile);
while(sc.hasNextLine())
{
names.add(sc.nextLine());
}
} catch (IOException ioe) { System.out.println("Failed to load file"); }
try
{
Scanner sc = new Scanner(numbFile);
while(sc.hasNextLine())
{
numbers.add(sc.nextLine());
}
} catch (IOException ioe) { System.out.println("Failed to load file"); }
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, names);
listview.setAdapter(adapter);
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name, number;
name = nameEdit.getText().toString();
number = numbEdit.getText().toString();
if(!name.equals("") && !number.equals(""))
{
names.add(name);
numbers.add(number);
adapter.notifyDataSetChanged();
try {
FileOutputStream nameOut = new FileOutputStream(nameFile, true);
OutputStreamWriter osw = new OutputStreamWriter(nameOut);
osw.write(name + "\n");
osw.flush();
osw.close();
FileOutputStream numbOut = new FileOutputStream(numbFile, true);
OutputStreamWriter osw2 = new OutputStreamWriter(numbOut);
osw2.write(number + "\n");
osw2.flush();
osw2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
delBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
答案 0 :(得分:1)
&#34;名称&#34;用于备份您的底层ArrayAdapter数据集 - 当您调用adapter.notifyDataSetChanged()时,您告诉您的适配器其底层数据存储已经发生了某些变化,它应该尝试重建列表。
不幸的是,StableArrayAdapter的getItemId(position)方法由HashMap支持,只有在首次构建适配器时才会更新。
因此,如果您向名称添加内容,但地图没有更新以反映...您将拥有NullPointerExceptions,因为地图没有相应的对象:
return mIdMap.get(item);