0

こんにちは、この初心者の質問をご覧いただきありがとうございます。

私の問題に関連するあらゆる種類の答えを探しましたが、私が抱えている問題を見つけることができないか、私が間違っていることを誰かに説明してもらう必要があるかもしれません.

とにかく、私の問題は、インテントを学習し、putExtra、getExtra などのアクティビティ間でデータを渡しようとしていることです。そのため、選択した日付を日付ピッカーから新しいアクティビティに渡すだけの機能を備えた非常に単純なアプリを作成しました。そしてそれを画面に表示します。ボタンをクリックして次のアクティビティを開始すると「android.content.res.Resources$NotFoundException」が発生するので、何か間違ったことをしているに違いありません。他の回答された質問とは異なるコードを試しましたが、役に立ちませんでした。 ?. 私のコードは以下のとおりです。ご回答いただきありがとうございます。

最初の活動

package date.Picker;




import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;   


public class DatePickerActivity extends Activity  {           

 int year;  
 int month;  
 int day; 

Button selectButton;
DatePicker datePicker;


@Override    
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);         
    setContentView(R.layout.main);                   

    datePicker = (DatePicker) findViewById(R.id.datePicker);
    selectButton = (Button) findViewById(R.id.selectButton);
    selectButton.setOnClickListener(new OnClickListener()
    {           

public void onClick(View v){         

    Intent intent = new Intent(getApplicationContext(),        datePickerResult.class);

    month = datePicker.getMonth();  
    day = datePicker.getDayOfMonth(); 
    year = datePicker.getYear();


    intent.putExtra("year",year);  
    intent.putExtra("month",month);  
    intent.putExtra("day",day);  

    startActivity(intent);


    } 

    });
}
 }

第二の活動

package date.Picker;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

 public class datePickerResult extends Activity {

 int year;  
 int month;  
 int day; 

 TextView textView1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dpresult);

textView1 = (TextView)findViewById(R.id.textView1); 

Bundle extras = getIntent().getExtras();  

    extras.getBundle("year"); 
    extras.getBundle("day");  
    extras.getBundle("month");  


    textView1.setText(year);
 }
}

ですから、あなたが持っている答えや助けに感謝します。少し時間の無駄だったらごめんなさい。

4

1 に答える 1

1

最初に表示されるのは、次のように呼び出したことです。

textView1.setText(year);

コンパイラはをリソース ID として認識しint year、それを ID として持つリソースを見つけようとします。できないので諦めます。代わりに、次の 2 つの行のいずれかを試してください。

textView1.setText(year+"");
textView1.setText(Integer.valueOf(year));

次に、次の戻り値を割り当てていません。

extras.getBundle("year"); 
extras.getBundle("day");  
extras.getBundle("month");

あなたがする必要があるのは、余分なものを選び、それを変数に割り当てることです。そのようです:

year = extras.getInt("year");
day = extras.getInt("day");
month = extras.getInt("month");

Bundleから年/日/月の値を正しく取得する必要がありますIntent

于 2012-06-25T00:05:38.223 に答える