[英]Android studio- How to display text page A and B from assets folder in a click of Button1 and Button2 resp
While executing im getting error like "Unfortunately your app is stopped". 执行即时讯息时,出现类似“不幸的是您的应用已停止”的错误。 I want to open my text or html file once the button is clicked. 单击按钮后,我想打开我的文本或html文件。
MainActivity.java MainActivity.java
package com.example.akarsh.aaa;
import android.graphics.drawable.Drawable;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtContent = (TextView) findViewById(R.id.tv1);
TextView txtFileName = (TextView) findViewById(R.id.tv2);
ImageView iv = (ImageView) findViewById(R.id.iv);
AssetManager assetManager = getAssets();
try {
String[] files = assetManager.list("Files");
for (int i = 0; i < files.length; i++) {
txtContent.append("\n Files=>" + i + "Name" + files);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
InputStream input;
try {
input = assetManager.open("text.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
String text = new String(buffer);
txtFileName.setText(text);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
InputStream in = assetManager.open("icon_android_small.png");
Drawable d = Drawable.createFromStream( in , null);
iv.setImageDrawable(d);
} catch (Exception e) {
// TODO: handle exception
return;
}
}
}
You need to implement the onClick-Method on your button 您需要在按钮上实现onClick-Method
Find the button in the activity_main
-File and set 在activity_main
-File中找到按钮并设置
android:onClick="onClick"
Then implement the onClick in the sourceCode of your MainActivity. 然后在MainActivity的sourceCode中实现onClick。 Like: 喜欢:
public void onClick(View view){
...
}
And in this method you open your file. 然后用这种方法打开文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.