无法获取使用Intent传递的值

时间:2010-12-01 23:37:18

标签: java android

将值从一个类传递到另一个类时遇到一些麻烦。我基本上有一个Timetable类,我想从中将一个值发送到另一个名为daytab的类。 我想要传递的值称为日。我试图通过使用intent.putExtra()传递它,但它不是发送值或不接收它。 它应该将接收到的变量存储在daytab.java

中的String daynew中

Timetable.java:


package day.tab;

import android.app.TabActivity;

import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost;

public class Timetable extends TabActivity {

private static final String EXTRA_DAY = "EXTRA_DAY";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)

    intent = new Intent().setClass(this, daytab.class);


    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Monday").setIndicator("Mon",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);

    intent.putExtra(EXTRA_DAY, "monday");
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Tuesday").setIndicator("Tue",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "tuesday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Wednesday").setIndicator("Wed",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "wednesday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Thursday").setIndicator("Thur",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "thursday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Friday").setIndicator("Fri",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "friday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    intent.putExtra(EXTRA_DAY, "saturday");
    spec = tabHost.newTabSpec("Saturday").setIndicator("Sat",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Sunday").setIndicator("Sun",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "sunday");
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

}

daytab.java.EXTRA_DAY:

private static final String EXTRA_DAY = "EXTRA_DAY";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)

    intent = new Intent().setClass(this, daytab.class);


    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Monday").setIndicator("Mon",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);

    intent.putExtra(EXTRA_DAY, "monday");
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Tuesday").setIndicator("Tue",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "tuesday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Wednesday").setIndicator("Wed",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "wednesday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Thursday").setIndicator("Thur",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "thursday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Friday").setIndicator("Fri",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "friday");
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    intent.putExtra(EXTRA_DAY, "saturday");
    spec = tabHost.newTabSpec("Saturday").setIndicator("Sat",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, daytab.class);
    spec = tabHost.newTabSpec("Sunday").setIndicator("Sun",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    intent.putExtra(EXTRA_DAY, "sunday");
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

的AndroidManifest.xml:



    


package day.tab;

import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter;

public class daytab extends ListActivity {

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;

private String daynew;
private NotesDbAdapter mDbHelper;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes_list);

    daynew = getIntent().getStringExtra("EXTRA_DAY");

    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());


    String[] hour = getResources().getStringArray(R.array.hours);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, hour));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                mDbHelper.open();
                fillData();
                registerForContextMenu(getListView());
        }
    });
}
/** Called when the activity is first created. */
private void fillData() {
    // Get all of the rows from the database and create the item list
    Cursor notesCursor = mDbHelper.fetchAllNotes(daynew);
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
    case INSERT_ID:
        createNote();
        return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deleteNote(info.id);
        fillData();
        return true;
    }
    return super.onContextItemSelected(item);
}

private void createNote() {
    Intent i = new Intent(this, NoteEdit.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, NoteEdit.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}

}


提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:2)

我看不到你在哪里初始化字符串day

旁注 - 我会a)使其名称全部为大写,因为它是常量(例如EXTRA_DAY),b)将其标记为静态最终。