私はプログラミングの初心者であり、基本的にはすべて自己教えています。 私が動作するようにいくつかのことを管理してきたが、今、私は次の問題が生じています:Android:インテントはonClickで動作していません
シナリオ: 私は2つの活動を持っています。 1つのアクティビティで、ボタンをクリックします。 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>
これを解決する方法や正しい情報を探す場所がわからないので、どこに間違っているのか分かります。私はちょうどコードを混乱させ、コードを一緒に壊して、どうにかしてそのほとんどを動かしました。この深いもので私は基本的に失われています。 そしてもう少し難しくするために、標準プログラミングの習慣やそれに付随する用語を正確には知らないので、簡単な説明と助けに感謝します。 お早めに感謝します。
あなたは、次の手順を確認する必要があり –
あなたのlogcatを確認してください。 1セットの直接 'intent.putExtra(EXTRA_MESSAGE、「これはテストメッセージです」);'必ず基本的なコードを動作させるために 2.ログ(Androidのモニター上を読みますIDEの場合)、ファイルのために例外が発生した場合はstackTraceが表示されます(存在しない、間違ったパス、許可...)。 3.レイアウトテキストボックスで、 – TranVo
提案していただきありがとうございます。私はいくつかのエラーメッセージを受け取り、それらを以下の返信に投稿しました。 – user2645886