2017-06-10 11 views
0

私はプログラミングの初心者であり、基本的にはすべて自己教えています。 私が動作するようにいくつかのことを管理してきたが、今、私は次の問題が生じています: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> 

これを解決する方法や正しい情報を探す場所がわからないので、どこに間違っているのか分かります。私はちょうどコードを混乱させ、コードを一緒に壊して、どうにかしてそのほとんどを動かしました。この深いもので私は基本的に失われています。 そしてもう少し難しくするために、標準プログラミングの習慣やそれに付随する用語を正確には知らないので、簡単な説明と助けに感謝します。 お早めに感謝します。

+0

あなたは、次の手順を確認する必要があり –

+1

あなたのlogcatを確認してください。 1セットの直接 'intent.putExtra(EXTRA_MESSAGE、「これはテストメッセージです」);'必ず基本的なコードを動作させるために 2.ログ(Androidのモニター上を読みますIDEの場合)、ファイルのために例外が発生した場合はstackTraceが表示されます(存在しない、間違ったパス、許可...)。 3.レイアウトテキストボックスで、 – TranVo

+0

提案していただきありがとうございます。私はいくつかのエラーメッセージを受け取り、それらを以下の返信に投稿しました。 – user2645886

答えて

1

ViewActivity.javaすべてがうまく見えるので、私はあなたのIndoorMenuActivity.javaをデバッグすると言います。
はログイン、あなたのコードをデバッグするこの助けを借りて仕事を学ぶ:
場所Log.e("MY_TEXT", "text=" + text);text = new String(buffer);
後に続いてAndroidのモニターをチェックして、「MY_TEXT」を探し、それが赤になります。 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(); 
     } 
+0

私は暴力を受けますが – user2645886

0

私はそこかもしれないと思いますあなたがInputStreamからコンテンツを読んでいる間に何か問題があります。ログの例外(キャッチブロック内)を確認し、再度チェックしてください。何らかのエラーがあった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(); 
} 
0

Welpからの読み出しデータのため、このコードを使用してください場合は、ちょうど私が愚か呼び出します。 -.- 私は)InputStreamの文字列は= getAssets(あるmisttyped ...エラーを検出しました。(「file.txtは」)オープン

私はこのフォーラムのためのテキストを編集しますが、コードの私を読めちょうど私がassetfolderで行ったように、文字列の最初の文字を資本で作るのを忘れてしまったことに気付きました。

いずれにしても、情報と助けをいただきありがとうございます。 これは今行われているように機能します。

関連する問題