2013-05-11 4 views
0

하나의 활동에 파일로 내용을 쓰고 다른 활동으로 읽으려는 시도를하고 있습니다. 사실, 사용자는 수신자의 번호를 한 번 입력해야합니다. 그 후, 난 내 오류이고, 내가 어제부터 시도 ... 자동 그를 위해 내가 모르는하나의 활동으로 파일을 작성하고 다른 활동에서 읽음

을이받는 사람의 번호를 입력합니다 ... 은 .. 당신의 도움을 주셔서 대단히 감사

package com.example.automatik;

공용 클래스 AddRecipientNumber는 활동 {

//On stocke le n° du destinataire (1) 
String FILENAME = "recipientnumber.txt"; 
String mehmet = "mehmetmehmet"; 
FileOutputStream fos; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add_recipient_number); 
    // Show the Up button in the action bar. 
    setupActionBar(); 

    Button button = (Button)findViewById(R.id.recipientbutton1); 
    button.setOnClickListener(new View.OnClickListener() { 

     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(final View v) 
     { 
      EditText textA = (EditText)findViewById(R.id.recipientedit1); 
      String a = textA.getText().toString(); 
      EditText textB = (EditText)findViewById(R.id.recipientedit2); 
      String b = textB.getText().toString(); 

      if(a.equals(b)) 
      { 
       //On stocke le n° du destinataire (2) 
       try 
       { 
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
        fos.write(mehmet.getBytes()); 
        fos.close(); 
       } 
       catch (FileNotFoundException e) { e.printStackTrace(); } 
       catch (IOException e) { e.printStackTrace(); } 

       AlertDialog alertdialog = new AlertDialog.Builder(AddRecipientNumber.this).create(); 
       alertdialog.setMessage("You entered these numbers correctly.\nNow, we will move to the second step!"); 
       alertdialog.setButton("Move", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) 
        { 
         sendMove(v); 
        } 
       }); 
       alertdialog.show(); 
      } 
      else 
      { 
       AlertDialog alertdialog = new AlertDialog.Builder(AddRecipientNumber.this).create(); 
       alertdialog.setMessage("These numbers must be identical. \nPlease try again."); 
       alertdialog.show(); 
      } 
     } 
    }); 
} 

//Une fois que les numéros entrés sont identiques & que l'utilisateur accepte de passer à la 2nde étape, on lance: 
public void sendMove(View v) 
{ 
    Intent intent = new Intent(this, AddMasterNumber.class); 
    startActivity(intent); 
} 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
private void setupActionBar() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

내 두 번째 활동 확장 :

패키지 com.example.automatik을;

공용 클래스 AddMasterNumber 당신은 당신이 달성하고자하는 것을 위해 파일을 필요로하지 않는 활동 {

String finall; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add_master_number); 
    // Show the Up button in the action bar. 
    //setupActionBar(); 

    //On récupère le n° du destinataire stocké 
    try 
    { 
     FileInputStream in = openFileInput("recipientnumber.txt"); 
     StringBuffer fileContent = new StringBuffer(""); 

     byte[] buffer = new byte[1024]; 

     while(in.read(buffer) != -1) 
     { 
      fileContent.append(new String(buffer)); 
     } 
     in.close(); 
     finall = fileContent.toString(); 
    } 
    catch (FileNotFoundException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); } 

    TextView text = (TextView)findViewById(R.id.recipienttext3); 
    text.setText(finall); 
} 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
private void setupActionBar() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

+0

후 로그 캣 오류 출력. 그러면 오류의 위치가 표시됩니다. – Neoh

+0

원하는 것을 달성하려고합니다. 대신에 sharedpreferences를 사용할 수 없습니까? 또는 응용 프로그램 클래스와 원하는 위치에서 액세스 할 수 있습니까? – qwr

+0

QWR : 한 활동에 파일을 쓰고 다른 활동으로 읽으려고합니다 ... 예를 들어 첫 활동에서 사용자가 수신자 번호를 입력하고 & nbsp; 그 후, 사용자가 친구에게 SMS를 보낼 필요가있을 때마다 나는 그 사람의 수를 자동으로 입력합니다 ... – d3vpasha

답변

1

을 확장합니다.

처음에는 ActivityIntent.putExtra을 사용하고 다른 하나에는 getIntent().getStringExtra(String)을 사용하십시오.

편집 : 대신

사용 SharefPreferences : http://developer.android.com/guide/topics/data/data-storage.html#pref

+0

의도를 사용하는 방법 등을 알고 있습니다.하지만이 수신자의 전화 번호를 입력해야하기 때문에 파일을 사용하고 싶습니다. 그가 처음이 응용 프로그램을 사용할 때 번호. 그 후에, 나는이 수령인의 번호를 저장하고 나의 EditText에 그것을 자동적으로 입력하고 사용자 물건을 용이하게하고 싶습니다. – d3vpasha

+0

인 텐트는 두 응용 프로그램 사이에서만 데이터를 공유하지만이 데이터도 저장하려고합니다. – d3vpasha

+0

@ forgive90 QWR에서 제안한 SharedPreferences보다 파일을 직접 조작하는 것보다 작업하기가 쉽습니다. –

관련 문제