gpt4 book ai didi

android - 创建 ArrayAdapter 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 16:04:13 25 4
gpt4 key购买 nike

我不知道为什么会出现 NullPointerException。我认为问题产生于 ArrayAdapter。需要帮忙。提前致谢。

LogCat:

android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)

Activity :

package com.map.map;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
Map map;
String Source = "Blank";
String Destination = "Blank";
ListView list1;
ListView list2;
String [] listArray;
BufferedReader a;
BufferedReader b;

public static Integer Number_Of_Airport = 1168; //Total number of Airport in the World
public HashMap<Integer,Time> Clock; //The Clock of each Airport
public HashMap<String,Integer> Airport; //airport mapped to representative integer
public HashMap<String,Coordinate> Coordinate; // airport mapped to their co-ordinates
public HashMap<String,ArrayList<IntegerFour>> AirportInfo; // airport mapped to their information
public ArrayList<String> shortestPath; // the list of coordinates indicates the way to shortest path
public HashMap<Integer,String> AirportName; //index mapped to the Airport
String [] listAirport; // holds the Airports name
int [] Parent; // For getting the ShortestPath way Parent keep track the Child



@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
a = new BufferedReader(
new InputStreamReader(getAssets().open("a.txt")));
} catch (IOException e) {
e.printStackTrace();
}

try {
b = new BufferedReader(
new InputStreamReader(getAssets().open("b.txt")));
} catch (IOException e) {
e.printStackTrace();
}

listArray = new String[1168];
AirportName = new HashMap<Integer,String>();
Airport = new HashMap<String,Integer>();
Coordinate = new HashMap<String,Coordinate>();
AirportInfo = new HashMap<String,ArrayList<IntegerFour>>();
shortestPath = new ArrayList<String>();
Clock = new HashMap<Integer,Time>();
listAirport = new String[Number_Of_Airport];
Parent = new int[Number_Of_Airport];



String line1;
int c = 0;// location detector
try{
while((line1 = AirportLocation.readLine())!=null){
StringTokenizer s = new StringTokenizer(line1);
String airport = s.nextToken();
//AirportNameByString.put(c, airport);
listAirport[c] = airport;
Airport.put(airport,c);
XY a = new XY(s.nextToken()); // setting the coordinates in exact form
int x = a.x;int y = a.y;
AirportName.put(c,airport);
Coordinate.put(airport, new Coordinate(x,y));
c++;// next Airport
}
}
catch (IOException e) {
e.printStackTrace();
}



String line2;
try{
while((line2 = AirportInformation.readLine())!=null){
String airportName = line2;
ArrayList<IntegerFour> list = new ArrayList<IntegerFour>();
for(int t = 0;t<3;t++){
StringTokenizer info = new StringTokenizer(AirportInformation.readLine());
String destinationAirport = info.nextToken();
Time slotTime = new Time(info.nextToken());
Time duration = new Time(info.nextToken());
int expense = Integer.valueOf(info.nextToken().replace("$",""));
list.add(new IntegerFour(destinationAirport,slotTime,duration,expense));
}
AirportInfo.put(airportName,list);
}
}
catch (IOException e) {
e.printStackTrace();
}




list1 = (ListView) findViewById(R.id.list1);
list2 = (ListView) findViewById(R.id.list2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
list1.setAdapter(adapter1);
list2.setAdapter(adapter2);
list1.setOnItemClickListener(new SourceSelection());
list2.setOnItemClickListener(new DestinationSelection());

}

public void shortestPath(){



int [] shortestpath = new int[Number_Of_Airport];
Arrays.fill(shortestpath,1000000000);
PriorityQueue<Pair> q = new PriorityQueue<Pair>();
q.add(new Pair(Source,0));
shortestpath[Airport.get(Source)] = 0;

Time starting = new Time("12Hour13Minute19Second");
Clock.put(Airport.get(Source),starting);
while(!q.isEmpty()){
String u = q.poll().airport;
if(!u.equals(Destination)){
ArrayList<IntegerFour> listOfInfo = AirportInfo.get(u);
for(int i = 0;i<listOfInfo.size();i++){
String airport = listOfInfo.get(i).destinationAirport;
int expense = listOfInfo.get(i).expense;
Time slotTime = listOfInfo.get(i).slotTime;
Time duration = listOfInfo.get(i).duration;
if(shortestpath[Airport.get(airport)]>shortestpath[Airport.get(u)]+expense){
Parent[Airport.get(airport)] = Airport.get(u);
Clock.put(Airport.get(airport),slotTime);
shortestpath[Airport.get(airport)] = shortestpath[Airport.get(u)]+expense;
q.add(new Pair(airport,shortestpath[Airport.get(airport)]));
}
}
}
else{
break;
}
}
}

public void getTheShortestPath(){
int start = Airport.get(Destination);
while(start!=Airport.get(Source)){
shortestPath.add(AirportName.get(start));
start = Parent[start];
}
shortestPath.add(AirportName.get(start));
}




public void Draw(){
map = new Map(this);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(map);
setContentView(scrollView);
//setContentView(map);
}

class SourceSelection implements AdapterView.OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView temp = (TextView) arg1;
Source = temp.getText()+"";
if(!Source.equals("Blank") && !Destination.equals("Blank")){
//shortestPath();
Draw();
}
}
}

class DestinationSelection implements AdapterView.OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView temp = (TextView) arg1;
Destination = temp.getText()+"";
if(!Source.equals("Blank") && !Destination.equals("Blank")){
//shortestPath();
Draw();
}
}
}
@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;
}
public class Map extends View {
Paint paint = new Paint();
public Map(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawCircle(100, 100, 20, paint);
canvas.drawLine(2000, 2000, 5000, 5000, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Compute the height required to render the view
// Assume Width will always be MATCH_PARENT.
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
setMeasuredDimension(width, height);
}
}
}


class Coordinate{
int x,y;
public Coordinate(int x,int y){
this.x = x;
this.y = y;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Coordinate other = (Coordinate) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}


class Time{
int hour,minute,second;
int totalSecond;
public Time(String s){
s = s.replace("Hour"," ");s = s.replace("Minute"," ");s = s.replace("Second"," ");
StringTokenizer ss = new StringTokenizer(s);
this.hour = Integer.valueOf(ss.nextToken());
this.minute = Integer.valueOf(ss.nextToken());
this.second = Integer.valueOf(ss.nextToken());
this.totalSecond = (this.second) + ((this.minute)*60) + ((this.hour)*3600);
}
}


class IntegerFour{
String destinationAirport;
Time slotTime;// in hour,minute,second 3 of them
Time duration;
int expense;
public IntegerFour(String destinationAirport,Time slotTime,Time duration,int expense){
this.destinationAirport = destinationAirport;
this.duration = duration;
this.slotTime = slotTime;
this.expense = expense;
}
}



class Pair implements Comparable{
String airport;
int dollar;
public Pair(String airport,int dollar){
this.airport = airport;
this.dollar = dollar;
}

public int compareTo(Object o) {
return (this.dollar - ((Pair)o).dollar);
}

}



class XY{
int x,y;
public XY(String s){
s = s.replace("(","");s = s.replace(")","");s = s.replace(","," ");
StringTokenizer ss = new StringTokenizer(s);
x = Integer.valueOf(ss.nextToken()); y = Integer.valueOf(ss.nextToken());
}
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<ListView
android:id="@+id/list1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" />

<ListView
android:id="@+id/list2"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />

<View
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>

最佳答案

您收到此错误是因为 listArray 的元素为空。在 Java 中,当你初始化一个数组时,它只是可以存储一些对象的东西。但是它的元素是null!您必须一个一个地设置数组元素。

关于android - 创建 ArrayAdapter 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20924961/

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