2013-07-03 96 views
0

我有一個名爲「Datetime」的類,我想使用intent將Datetime的活動調用到我的MainActivity中。使用意圖調用活動到另一個活動失敗

這是我做過什麼

public void onClick(View view) { 

     Intent i = new Intent(this, Datetime.class); 
     startActivity(i); 

}

,我也更新了我的清單:

<activity 
       android:name=".Datetime" > 
</activity> 

,但它不工作。我的代碼有什麼問題?

爲了以防萬一,你想參考。以下是我的DateTime類:

public class Datetime extends Activity { 

    static final int DATE_DIALOG_ID = 1; 
    static final int TIME_DIALOG_ID = 2; 

    private Button pickDate; 
    private int year, month, day; 

    private Button pickTime; 
    private int hours, min; 

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


     pickDate = (Button)findViewById(R.id.date_button); 

     pickDate.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       showDialog(DATE_DIALOG_ID); 
      } 

     }); 

     final Calendar cal = Calendar.getInstance(); 
     year = cal.get(Calendar.YEAR); 
     month = cal.get(Calendar.MONTH); 
     day = cal.get(Calendar.DAY_OF_MONTH); 

     updateDate(); 


     pickTime = (Button)findViewById(R.id.time_button); 

     pickTime.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       showDialog(TIME_DIALOG_ID); 

      } 

     }); 

     hours = cal.get(Calendar.HOUR); 
     min = cal.get(Calendar.MINUTE); 

     updateTime(); 
    } 

    private void updateTime() { 
     pickTime.setText(new StringBuilder().append(hours).append(':') 
       .append(min)); 

    } 

    private void updateDate() { 
     pickDate.setText(new StringBuilder().append(day).append('-') 
       .append(month + 1).append('-').append(year)); 

    } 

    private DatePickerDialog.OnDateSetListener dateListener = 
     new DatePickerDialog.OnDateSetListener() { 

      @Override 
      public void onDateSet(DatePicker view, int yr, int monthOfYear, 
        int dayOfMonth) { 
       year = yr; 
       month = monthOfYear; 
       day = dayOfMonth; 
       updateDate(); 
      } 
    }; 

    private TimePickerDialog.OnTimeSetListener timeListener = 
     new TimePickerDialog.OnTimeSetListener() { 

      @Override 
      public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
       hours = hourOfDay; 
       min = minute; 
       updateTime(); 
      } 

    }; 
    protected Dialog onCreateDialog(int id){ 
     switch(id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, dateListener, year, month, day); 
     case TIME_DIALOG_ID: 
      return new TimePickerDialog(this, timeListener, hours, min, false); 
     } 
     return null; 

    } 
} 

的logcat:

07-03 16:36:37.394: E/Dynamiclayout(1282): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0 
07-03 16:36:37.394: E/Dynamiclayout(1282): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0 
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
07-03 16:36:37.424: E/SpannableStringBuilder(1282): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
07-03 16:36:42.189: D/GestureDetector(1282): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0 

按照要求,我MainActivity類別:

public class MainActivity extends TabActivity { 

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

     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) 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     intent = new Intent().setClass(this, ViewTab.class); 

     spec = tabHost.newTabSpec("View").setIndicator("View", 
          res.getDrawable(R.drawable.ic_tab_view)) 
         .setContent(intent); 
     tabHost.addTab(spec); 


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

     // Do the same for the other tabs 
     spec = tabHost.newTabSpec("Create").setIndicator("Create", 
          res.getDrawable(R.drawable.ic_tab_create)) 
         .setContent(intent); 
     tabHost.addTab(spec); 



     tabHost.setCurrentTab(2); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
     case R.id.menu_about: 
      startActivity(new Intent(this,About.class)); 
      return true; 
     } 

     return false; 
    } 


    public void onClick(View view) { 

     Intent i = new Intent(this, Datetime.class); 
     startActivity(i); 


     } 


} 
+3

你宣佈日期時間在Manfiest? – Blackbelt

+4

給我們stacktrace –

+0

是的,我宣佈了。請看看我的logcat。謝謝。 –

回答

0

我的猜測是,你可能會忘記監聽器添加到您的按鈕

public class MainActivity extends Activity implements OnClickListener{ 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.create_layout); 
     Button btn = (Button)findViewById(yourId); 
     btn.setOnClickListener(this); 
} 
} 

Yo ü必須在一個視圖設置爲監聽點擊

更新:

TabActivity被depricated。如果你去了Android開發人員指南,他們告訴你如何可以做到這一點

http://developer.android.com/reference/android/app/TabActivity.html

有示例代碼,告訴您如何做到這一點使用片段

+0

嗨。感謝您的回答,但我的MainActivity已經擴展了Tabactivity。我應該如何插入你的代碼? –

+0

在哪種情況下你需要打電話給你的意圖 –

+0

然後你可以使用這段代碼 –

相關問題