gpt4 book ai didi

java - 构造ArrayAdaptor时出现Resources.NotFoundException

转载 作者:行者123 更新时间:2023-12-01 11:11:21 28 4
gpt4 key购买 nike

这是我的 .java 文件:

package com.example.rohitkulkarni.readingfile;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listView= (ListView) findViewById(R.id.listView);
try{

InputStream inputreader = getAssets().open("myaddress.txt");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader));



ArrayList<String> lines = new ArrayList<String>();
boolean hasNextLine =true;
while (hasNextLine){

String line = buffreader.readLine();
lines.add(line);
hasNextLine = line != null;


}
tv.setText((CharSequence) lines);

//exception here:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.id.listView,lines);

listView.setAdapter(adapter);

inputreader.close();

}
catch(FileNotFoundException e){

}catch(IOException e){

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

这是我的 xml 文件:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

我正在使用 getAsset 一切正常,但只有 ArrayAdaptor 不起作用我尝试了所有可能的组合,但它不起作用。它使用简单的字符串数组。

最佳答案

字符串行= buffreader.readLine();

//我发现他取的是空值,因此检查它

if(line != null);

            lines.add(line);
hasNextLine = line != null;

//不是工作正常,谢谢

关于java - 构造ArrayAdaptor时出现Resources.NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32326331/

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