Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用意图将活动调用到另一个活动失败_Android_Android Intent - Fatal编程技术网

Android 使用意图将活动调用到另一个活动失败

Android 使用意图将活动调用到另一个活动失败,android,android-intent,Android,Android Intent,我有一个名为“Datetime”的类,我想使用intent将Datetime的活动调用到我的MainActivity中 这就是我所做的 public void onClick(View view) { Intent i = new Intent(this, Datetime.class); startActivity(i); } 我还更新了我的清单: <activity android:name=".Datetime" &g

我有一个名为“Datetime”的类,我想使用intent将Datetime的活动调用到我的MainActivity中

这就是我所做的

public void onClick(View view) {

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

我还更新了我的清单:

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

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
根据要求,我的主要活动课程:

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);


        }


}

我猜你可能忘了在按钮中添加侦听器

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);
}
}
必须将一个视图设置为单击侦听器

更新:

这项活动是去润滑的。如果你去看Android开发者指南,他们会告诉你如何做到这一点


有一个示例代码向您展示了如何使用片段来实现这一点

您是否在Manfiest中声明了Datetime?是的,我已经声明了。请也看看我的日志。谢谢。请贴上你的全票logcat@EtAndrea也许可以从logcat获得更多信息?将您的主要活动发布到Hi。谢谢你的回答,但是我的主要活动已经扩展了我的活动。我应该如何插入您的代码?在何种情况下,您需要调用您的意图
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);
}
}