gpt4 book ai didi

java - 仅在平板电脑上发生 Android 致命异常

转载 作者:行者123 更新时间:2023-12-01 14:36:01 25 4
gpt4 key购买 nike

我有一个项目可以在 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;
}
}
}

最佳答案

“names”被用来支持你的底层ArrayAdapter数据集 - 当你调用adapter.notifyDataSetChanged()时,你告诉你的适配器它的底层数据存储已经发生了某种变化,它应该尝试重建列表。

不幸的是,StableArrayAdapter 的 getItemId(position) 方法由 HashMap 支持,您仅在首次构建适配器时更新该 HashMap。

因此,如果您向名称添加某些内容,但 map 没有更新以反射(reflect)这一点...您将遇到 NullPointerExceptions,因为 map 没有对应的对象:

return mIdMap.get(item);

关于java - 仅在平板电脑上发生 Android 致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16483177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com