I am creating an application with eclipse in Android. My software was working flawlessly until I created a function that created a new intent back to MainActivity from one of my tabbed Activiy then it stopped working, giving the 'force closed application' error whenever I ran it. I have since deleted that function but the error persists,
我正在Android中使用eclipse创建一个应用程序。我的软件运行得非常完美,直到我创建了一个函数,该函数从我的一个选项卡式活动创建了一个新的意图返回到主活动,然后它就停止了工作,每当我运行它时,就会出现“强制关闭应用程序”错误。我已经删除了那个函数但是错误仍然存在,
here is my code,
这是我的代码,
package my.main;
包my.main;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
HttpClient client = new HttpClient();
Security security = new Security();
//file name for storing persistent data for sessions
public static final String PREFS_SESSION = "AccountsFile";
public static String storedEmailName = "userEmail";
public static String storedPasswordName = "userPassword";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*check if username has been stored*/
// if stored info exists, log the user in with login details and display main layout
if(doesPersistExist(storedEmailName, PREFS_SESSION)){
// auto login user
try
{autologin();}
catch (UnsupportedEncodingException e)
{e.printStackTrace();}
setContentView(R.layout.tabbed_main);
// Once logged in, display tabbed layouts
// Generate tabes suing function generateTags()
// Generate tabs
generateTab("Scan", R.drawable.icon_tab_scan, ScanActivity.class);
generateTab("Recent", R.drawable.icon_tab_recent, RecentActivity.class);
generateTab("Favourites", R.drawable.icon_tab_favourites, FavouritesActivity.class);
generateTab("Settings", R.drawable.icon_tab_settings, SettingsActivity.class);
}
// else, username has not been stored, prompt login
else{
setContentView(R.layout.login);
}
}
// Auto login user if credentials already exist
public void autologin() throws UnsupportedEncodingException{
//deleted contents of function to save room - this function definitely works
}
// Function that logs a user out by deleting the store user credentials
public void logout(){
//Create a sharedPreferences instance
SharedPreferences persist = getSharedPreferences(PREFS_SESSION, 0);
// Delete stored username and password
persist.edit().remove(storedPasswordName).commit();
persist.edit().remove(storedEmailName).commit();
// Rteurn user to login screen
setContentView(R.layout.login);
}
/*storeString*/
//Persistently store a string - used for cookie name and value strings
public Boolean storeString(String PREF_NAME, String stringName, String stringValue){
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREF_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//commit session name and value to memory
editor.putString(stringName, stringValue);
// Commit the edits
Boolean result = editor.commit();
return result;
}
/*retrieveStoredString*/
//retrieve a persistently stored string from function storeString()
public String retrieveStoredString(String PREF_NAME, String stringName){
SharedPreferences persist = getSharedPreferences(PREF_NAME, 0);
String result = persist.getString(stringName, null);
return result;
}
// Check if persistent store exists
public Boolean doesPersistExist(String key, String PREF_NAME){
SharedPreferences persist = getSharedPreferences(PREF_NAME, 0);
Boolean result = persist.contains(key);
return result;
}
// Function will generate a new tab
public void generateTab(String tabSpec, int id, Class activity){
TabHost tabHost = getTabHost();
// Tab for scan
TabSpec scanspec = tabHost.newTabSpec(tabSpec);
// setting Title and Icon for the Tab
scanspec.setIndicator(tabSpec, getResources().getDrawable(id));
// create intent to start new tabbed activity
Intent scanIntent = new Intent(this, activity);
scanspec.setContent(scanIntent);
// add the tab to the tab layout
tabHost.addTab(scanspec);
}
}
This is my tabbed_main.xml
这是我tabbed_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">
<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"/>
</LinearLayout>
</TabHost>
I have created the in Manifest, as I said, the tabbed layout worked perfectly until I tried to add that function and since then nothing has worked. This is the logs from logcat,
我已经创建了in Manifest,就像我说的,选项卡式布局工作得很好,直到我尝试添加那个函数,从那以后就没有任何工作了。这是logcat的日志,
08-08 22:11:42.390: D/dalvikvm(1966): GC_FOR_ALLOC freed 37K, 5% free 6387K/6659K, paused 32ms
08-08 22:11:42.390: I/dalvikvm-heap(1966): Grow heap (frag case) to 6.834MB for 513744-byte allocation
08-08 22:11:42.440: D/dalvikvm(1966): GC_FOR_ALLOC freed 8K, 5% free 6881K/7175K, paused 35ms
08-08 22:11:42.490: D/AndroidRuntime(1966): Shutting down VM
08-08 22:11:42.490: W/dalvikvm(1966): threadid=1: thread exiting with uncaught exception (group=0x40226760)
08-08 22:11:42.490: E/AndroidRuntime(1966): FATAL EXCEPTION: main
08-08 22:11:42.490: E/AndroidRuntime(1966): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.main/my.main.MainActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.os.Looper.loop(Looper.java:132)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread.main(ActivityThread.java:4028)
08-08 22:11:42.490: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 22:11:42.490: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:491)
08-08 22:11:42.490: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-08 22:11:42.490: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-08 22:11:42.490: E/AndroidRuntime(1966): at dalvik.system.NativeStart.main(Native Method)
08-08 22:11:42.490: E/AndroidRuntime(1966): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
08-08 22:11:42.490: E/AndroidRuntime(1966): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:245)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.Activity.setContentView(Activity.java:1780)
08-08 22:11:42.490: E/AndroidRuntime(1966): at my.main.MainActivity.onCreate(MainActivity.java:57)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-08 22:11:42.490: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
08-08 22:11:42.490: E/AndroidRuntime(1966): ... 11 more
08-08 22:11:42.500: D/dalvikvm(1966): GC_CONCURRENT freed 61K, 4% free 7021K/7239K, paused 2ms+4ms
08-08 22:16:46.110: I/Process(1966): Sending signal. PID: 1966 SIG: 9
08-08 22:20:10.500: V/TLINE(2142): new: android.text.TextLine@40871fb0
08-08 22:20:10.530: V/TLINE(2142): new: android.text.TextLine@40872bf8
08-08 22:22:19.030: I/jdwp(2142): Ignoring second debugger -- accepting and dropping
08-08 22:22:21.210: I/jdwp(2205): Ignoring second debugger -- accepting and dropping
08-08 22:22:21.310: D/dalvikvm(2205): GC_FOR_ALLOC freed 39K, 5% free 6385K/6659K, paused 34ms
08-08 22:22:21.310: I/dalvikvm-heap(2205): Grow heap (frag case) to 6.833MB for 513744-byte allocation
08-08 22:22:21.370: D/dalvikvm(2205): GC_CONCURRENT freed 0K, 5% free 6887K/7175K, paused 3ms+2ms
08-08 22:22:21.400: D/AndroidRuntime(2205): Shutting down VM
08-08 22:22:21.400: W/dalvikvm(2205): threadid=1: thread exiting with uncaught exception (group=0x40226760)
08-08 22:22:21.410: E/AndroidRuntime(2205): FATAL EXCEPTION: main
08-08 22:22:21.410: E/AndroidRuntime(2205): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.main/my.main.MainActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.os.Looper.loop(Looper.java:132)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread.main(ActivityThread.java:4028)
08-08 22:22:21.410: E/AndroidRuntime(2205): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 22:22:21.410: E/AndroidRuntime(2205): at java.lang.reflect.Method.invoke(Method.java:491)
08-08 22:22:21.410: E/AndroidRuntime(2205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-08 22:22:21.410: E/AndroidRuntime(2205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-08 22:22:21.410: E/AndroidRuntime(2205): at dalvik.system.NativeStart.main(Native Method)
08-08 22:22:21.410: E/AndroidRuntime(2205): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
08-08 22:22:21.410: E/AndroidRuntime(2205): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:245)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.Activity.setContentView(Activity.java:1780)
08-08 22:22:21.410: E/AndroidRuntime(2205): at my.main.MainActivity.onCreate(MainActivity.java:57)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-08 22:22:21.410: E/AndroidRuntime(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
08-08 22:22:21.410: E/AndroidRuntime(2205): ... 11 more
08-08 22:27:25.039: I/Process(2205): Sending signal. PID: 2205 SIG: 9
It looks like "Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' error with" is the major error here, but from looking online I have found that this is usually down to not setting the id tag in the 'tabbed_main.xml' properly but I have definitely set that right. I have spent hours looking online and through code but am clueless to why it doesn't work. Any help would be greatly appreciated! Thanks.
它看起来像是“您的内容必须有一个id属性为'android.R.id '的TabHost。tabhost' error with"是这里的主要错误,但是通过在线查找,我发现这通常是由于没有在'tabbed_main '中设置id标记。xml是正确的,但我确实设置对了。我花了几个小时在网上和代码中查找,但我不知道为什么它不能工作。如有任何帮助,我们将不胜感激!谢谢。
3 个解决方案
#1
3
Right, so I managed to find out what the problem is, so I thought I would share. The problem was down to using SharedPreferences functions within TabActivity. When I changed the extend to Activity and created a new MainTabbedActivity to generate the tabs for the tab layout everything worked fine.
是的,我找到了问题所在,所以我想分享一下。问题在于在TabActivity中使用SharedPreferences函数。当我将扩展改为Activity并创建一个新的MainTabbedActivity以生成选项卡布局的选项卡时,一切都运行得很好。
I'm not 100% sure why this is but I guess it might have something to do with Context. I was most probably using TabActivity wrong as all that activity is supposed to do is generate tabs for the tablayout - not execute any functions. (if anyone knows the exact reason it doesn't work please feel free to comment, I would love to know!)
我不能百分之百地确定这是为什么,但我猜这可能与上下文有关。我很可能是用错了TabActivity,因为该活动所要做的就是为tablayout生成选项卡——而不是执行任何函数。(如果有人知道它不工作的确切原因,请随意评论,我很想知道!)
So for anyone with similar problems in the future, just do whatever you need to do in your main activity, then create an intent to your TabActivity like so,
所以对于将来遇到类似问题的人,只要在你的主要活动中做任何你需要做的事情,然后创建一个意图,像这样,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*check if username has been stored*/
// if stored info exists, log the user in with login details and display main layout
if(doesPersistExist(storedEmailName, PREFS_SESSION)){
// auto login user
// Once logged in, display tabbed layouts
Intent i = new Intent(getApplicationContext(), MainTabbedActivity.class);
startActivity(i);
}
// else, username has not been stored, prompt login
else{
setContentView(R.layout.login);
}
}
#2
0
Well,
好吧,
I'm really not sure, but according to this answer, once he replaced
我真的不确定,但根据这个答案,一旦他取代
android:id="@android:id/tabhost"
with
与
android:id="@+id/tabhost"
It worked for him.. give it a try.
它为他工作。试一试。
EDIT Since that didn't work, the answer below that one, indicated that he got this working by deleteing the R.java
file, and once the IDE regenerated it (maybe by rebuilding) it worked Good luck
编辑因为它不起作用,下面的答案,表明他通过删除R使它起作用。java文件,一旦IDE重新生成(可能通过重建),它就会运行良好。
#3
-1
Rename your xml file to something unique like main.xml or something else instead of tabbed_main.xml. Also clean your project and delete R file (it will be regenerated again) , sometimes it solves your problem.
将xml文件重命名为main之类的惟一文件。用xml代替tabbed_main.xml。还要清理项目并删除R文件(它将重新生成),有时它会解决您的问题。
#1
3
Right, so I managed to find out what the problem is, so I thought I would share. The problem was down to using SharedPreferences functions within TabActivity. When I changed the extend to Activity and created a new MainTabbedActivity to generate the tabs for the tab layout everything worked fine.
是的,我找到了问题所在,所以我想分享一下。问题在于在TabActivity中使用SharedPreferences函数。当我将扩展改为Activity并创建一个新的MainTabbedActivity以生成选项卡布局的选项卡时,一切都运行得很好。
I'm not 100% sure why this is but I guess it might have something to do with Context. I was most probably using TabActivity wrong as all that activity is supposed to do is generate tabs for the tablayout - not execute any functions. (if anyone knows the exact reason it doesn't work please feel free to comment, I would love to know!)
我不能百分之百地确定这是为什么,但我猜这可能与上下文有关。我很可能是用错了TabActivity,因为该活动所要做的就是为tablayout生成选项卡——而不是执行任何函数。(如果有人知道它不工作的确切原因,请随意评论,我很想知道!)
So for anyone with similar problems in the future, just do whatever you need to do in your main activity, then create an intent to your TabActivity like so,
所以对于将来遇到类似问题的人,只要在你的主要活动中做任何你需要做的事情,然后创建一个意图,像这样,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*check if username has been stored*/
// if stored info exists, log the user in with login details and display main layout
if(doesPersistExist(storedEmailName, PREFS_SESSION)){
// auto login user
// Once logged in, display tabbed layouts
Intent i = new Intent(getApplicationContext(), MainTabbedActivity.class);
startActivity(i);
}
// else, username has not been stored, prompt login
else{
setContentView(R.layout.login);
}
}
#2
0
Well,
好吧,
I'm really not sure, but according to this answer, once he replaced
我真的不确定,但根据这个答案,一旦他取代
android:id="@android:id/tabhost"
with
与
android:id="@+id/tabhost"
It worked for him.. give it a try.
它为他工作。试一试。
EDIT Since that didn't work, the answer below that one, indicated that he got this working by deleteing the R.java
file, and once the IDE regenerated it (maybe by rebuilding) it worked Good luck
编辑因为它不起作用,下面的答案,表明他通过删除R使它起作用。java文件,一旦IDE重新生成(可能通过重建),它就会运行良好。
#3
-1
Rename your xml file to something unique like main.xml or something else instead of tabbed_main.xml. Also clean your project and delete R file (it will be regenerated again) , sometimes it solves your problem.
将xml文件重命名为main之类的惟一文件。用xml代替tabbed_main.xml。还要清理项目并删除R文件(它将重新生成),有时它会解决您的问题。