Android Tab 教程问题
问题解决了!
在一遍又一遍地检查调试器之后,我刚刚发现了我的错误,但无论如何,谢谢大家!!
您好,我对 Android 很陌生,正在学习一些教程。现在我被 Tab 教程困住了。乍一看,代码似乎很好,因为我没有收到任何错误,但是当我尝试在模拟器上运行应用程序时,它总是崩溃,并且收到“应用程序意外停止”错误消息。
我已经尝试过关于 选项卡布局问题 的讨论中所说的内容,但是更改我所做的对错误没有帮助。如果有人能帮助我,那就太好了。如果您想查看代码,请告诉我是否应该发布或发送它。
提前谢谢你。
这是我的 Manifest.xml 文件代码,因为我不确定我是否在那里做错了什么:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilevideoeditor.moved"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".ShareGalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"> </activity>
<activity android:name=".EditGalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"> </activity>
</activity>
</application>
</manifest>
这是 GalleryView.java 文件:
package com.mobilevideoeditor.moved;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class GalleryView extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, EditGalleryView.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("edit").setIndicator("Edit",
res.getDrawable(R.drawable.ic_tab_edit))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ShareGalleryView.class);
spec = tabHost.newTabSpec("share").setIndicator("Share",
res.getDrawable(R.drawable.ic_tab_share))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
这是代码对于main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
EditGalleryView.java 和 ShareGalleryView.java 的代码:
package com.mobilevideoeditor.moved;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ShareGalleryView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Share tab!");
setContentView(textview);
}
}
最后是 ic_tab_share.xml 和 ic_tab_edit 的代码。 xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use ic_tab_share (grey icon) -->
<item android:drawable="@drawable/ic_tab_share"
android:state_selected="true" />
<!-- When not selected, use ic_tab_share_unselected (white icon)-->
<item android:drawable="@drawable/ic_tab_share_unselected" />
</selector>
再次感谢:)
PROBLEM SOLVED!!
I just found my mistakes, after looking through the debugger over and over again, but anyway, thanks everyone!!
Hi there, I am very new to Android and was working through some tutorials. Now I got stuck at the Tab tutorial. The code seems to be fine at first glance as I am not getting any errors, but when I try to run the app on the emulator it always crashes and I get an "application stopped unexpectedly" error message.
I already tried what was said in the discussion about the tab layout problem, but the changes I made didn't help with the error. If someone can help me with that, that would absolutely great. If you would like to see the code, just tell me if I should post or send it.
Thank u in advance.
This is my code for the Manifest.xml file because I am not sure, if I have done something wrong there:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilevideoeditor.moved"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".ShareGalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"> </activity>
<activity android:name=".EditGalleryView" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"> </activity>
</activity>
</application>
</manifest>
This is the GalleryView.java file:
package com.mobilevideoeditor.moved;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class GalleryView extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable 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, EditGalleryView.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("edit").setIndicator("Edit",
res.getDrawable(R.drawable.ic_tab_edit))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ShareGalleryView.class);
spec = tabHost.newTabSpec("share").setIndicator("Share",
res.getDrawable(R.drawable.ic_tab_share))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Here is the code for the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
The code for the EditGalleryView.java and the ShareGalleryView.java:
package com.mobilevideoeditor.moved;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ShareGalleryView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the Share tab!");
setContentView(textview);
}
}
And finally the code for the ic_tab_share.xml and the ic_tab_edit.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use ic_tab_share (grey icon) -->
<item android:drawable="@drawable/ic_tab_share"
android:state_selected="true" />
<!-- When not selected, use ic_tab_share_unselected (white icon)-->
<item android:drawable="@drawable/ic_tab_share_unselected" />
</selector>
Thanks again :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我第一次尝试官方开发人员网站的选项卡布局教程时,我遇到了类似的问题。
由于您在运行应用程序时遇到“强制关闭”错误,很可能是由于未在 AndroidManifest.xml 文件中添加活动所致。
I had a similar problem when I first tried the Tab Layout Tutorial from official developer's site.
Since you are getting the 'force close' error on running the application it would be most probably due to not adding the activities in AndroidManifest.xml file.