所以我是编程初学者,基本上只是自学一切。 我已经设法完成了一些工作,但现在我遇到了以下问题:
方案:
我有2个活动。在一个活动中,我点击一个按钮。 onClick
它应该从资源文件夹中的文本文件中以字符串格式检索文本并将其发送到下一个活动,并在其textView
字段中显示该字符串。
问题:
我现在拥有的是:我可以点击按钮,它会打开下一个活动,但我看不到任何文字。
我无法弄清楚出了什么问题。我试图在调试它的同时在按钮上设置breakline
,但是我无法查看调试器向我投掷的信息。 :(
我有以下代码:
活动1: IndoorMenuActivity.java
public class IndoorMenuActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_indoor_menu);
Button btn = (Button)findViewById(R.id.button66);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IndoorMenuActivity.this, ViewActivity.class);
String text = "";
try{
InputStream is = getAssets().open("file.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException ex) {
ex.printStackTrace();
}
intent.putExtra(EXTRA_MESSAGE, text);
startActivity(intent);
}
});
}
}
活动2: ViewActivity.java
public class ViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.myapp.MESSAGE");
// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.tv_text);
textView.setText(message);
}
}
我的清单文件: 的AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GeneralInformationActivity" />
<activity android:name=".IndoorMenuActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity android:name=".OutdoorMenuActivity" />
<activity android:name=".ViewActivity" />
<activity android:name=".DatabaseMenuActivity" />
<activity android:name=".ToolsMenuActivity" />
<activity android:name=".SplashActivity"></activity>
</application>
我不知道如何解决这个问题或者在哪里找到正确的信息,所以我可以看到我出错的地方。我只是将代码组合在一起并将其大幅削减,并以某种方式完成了大部分工作。但是有了这些更深层的东西,我基本上就输了。 并且使它变得更加困难:我不完全了解任何标准的编程实践或随附的术语,因此非常感谢一些简单的解释和帮助。 欢呼并提前感谢。
答案 0 :(得分:1)
在ViewActivity.java
中,一切看起来都不错,所以我说调试IndoorMenuActivity.java
学习使用Log,这有助于调试代码:
在Log.e("MY_TEXT", "text=" + text);
之后放置text = new String(buffer);
然后检查Android监视器并查找&#34; MY_TEXT&#34;,它将显示为红色
如果您没有看到它,那就意味着在阅读文件时出现问题,现在在Log.e("MY_TEXT", "below is the error");
之前添加另一个日志:ex.printStackTrace();
总结一下:
try{
InputStream is = getAssets().open("file.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
Log.e("MY_TEXT", "text=" + text);
} catch (IOException ex) {
`Log.e("MY_TEXT", "below is the error");
ex.printStackTrace();
}
答案 1 :(得分:0)
我认为在您从InputStream中读取内容时可能会出现一些问题。请检查日志中的异常(在Catch块中)并再次检查。如果有任何错误,请使用此代码从InputStream
读取数据public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
答案 2 :(得分:0)
Welp,只是叫我傻。 -.- 我发现了错误...... 我把输入字符串的字符串搞错了= getAssets()。open(&#34; file.txt&#34;)
我为这个论坛编辑了文本,但在阅读完代码后,我发现我忘了在资源文件夹中使用资本在字符串路径中创建第一个字母。
无论如何,感谢你的信息和帮助。 它现在可以正常运作。