本文将介绍java.lang.IllegalStateException:找不到线程绑定请求,方面是异常的详细情况,特别是关于java中错误:找不到或无法加载主类的相关信息。我们将通过案例分析、数据研
本文将介绍java.lang.IllegalStateException:找不到线程绑定请求,方面是异常的详细情况,特别是关于java中错误:找不到或无法加载主类的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ajax – java.lang.IllegalStateException:CDATA标签可能不嵌套、android – java.lang.IllegalStateException:找不到方法(View)、android – java.lang.IllegalStateException:没有活动、android – java.lang.IllegalStateException:活动已被破坏的知识。
本文目录一览:- java.lang.IllegalStateException:找不到线程绑定请求,方面是异常(java中错误:找不到或无法加载主类)
- ajax – java.lang.IllegalStateException:CDATA标签可能不嵌套
- android – java.lang.IllegalStateException:找不到方法(View)
- android – java.lang.IllegalStateException:没有活动
- android – java.lang.IllegalStateException:活动已被破坏
java.lang.IllegalStateException:找不到线程绑定请求,方面是异常(java中错误:找不到或无法加载主类)
以下是我的观点:
@Configurable @Aspect public class TimingAspect { @Autowired private HttpServletRequest httpServletRequest; // Generic performance logger for any mothod private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint, String remoteAddress) { StringBuilder tag = new StringBuilder(); if (joinPoint.getTarget() != null) { tag.append(joinPoint.getTarget().getClass().getName()); tag.append("."); } tag.append(joinPoint.getSignature().getName()); StopWatch stopWatch = new StopWatch(tag.toString()); Object result = joinPoint.proceed(); // continue on the intercepted method stopWatch.stop(); PerformanceUtils.logInPerf4jFormat(stopWatch.getStartTime(), stopWatch.getElapsedTime(), stopWatch.getTag(), stopWatch.getMessage(), remoteAddress); return result; } @Around("execution(* $$$.$$$.$$$.api.controller.*.*(..))") public Object logAroundApis(ProceedingJoinPoint joinPoint) throws Throwable { String remoteAddress = null; if (httpServletRequest != null) { remoteAddress = httpServletRequest.getRemoteAddr(); } return logPerfomanceInfo(joinPoint, remoteAddress); } @Around("execution(* $$$.$$$.$$$.$$$.$$$.$$$.*(..))") public Object logAroundService(ProceedingJoinPoint joinPoint) throws Throwable { String remoteAddress = null; if (httpServletRequest != null) { remoteAddress = httpServletRequest.getRemoteAddr(); } return logPerfomanceInfo(joinPoint, remoteAddress); }
我没有收到任何编译时错误,但是在启动码头服务器时出现以下异常:
嵌套异常为java.lang.IllegalStateException:未找到线程绑定的请求:您是在实际Web请求之外引用请求属性,还是在原始接收线程之外处理请求?如果您实际上是在Web请求中操作并且仍然收到此消息,则您的代码可能在DispatcherServlet
/
DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。
这里要注意的一件事是,如果删除“ logAroundService”方法,则不会获得任何异常。
答案1
小编典典您不应该HttpServletRequest
在方面方面自动进行a布线,因为这会将您的方面与只能在执行中调用的类一起运行HttpServletRequest
。
而是RequestContextHolder
在需要时使用来获取请求。
private String getRemoteAddress() { RequestAttributes attribs = RequestContextHolder.getRequestAttributes(); if (attribs instanceof NativeWebRequest) { HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest(); return request.getRemoteAddr(); } return null;}
ajax – java.lang.IllegalStateException:CDATA标签可能不嵌套
SEVERE: Servlet.service() for servlet Faces Servlet threw exception java.lang.IllegalStateException: CDATA tags may not nest at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.startCDATA(HtmlResponseWriter.java:630) at javax.faces.context.ResponseWriterWrapper.startCDATA(ResponseWriterWrapper.java:172) at javax.faces.context.PartialResponseWriter.startError(PartialResponseWriter.java:342) at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:210) at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200) at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:123) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
我认为这是String对象的一些问题,因为当我硬编码站点上显示的JPA实体属性时,一切都OK。然而,当从数据库(Postgresql)中检索实体时,它会引发上述异常。
JSF代码:
<p:column> <f:facet name="header"> Akcja </f:facet> <h:commandButton actionListener="#{mBDocumentMigration.actionEdit(object)}" value="Edytuj" rendered="#{mBDocumentMigration.editingObject == null}" > <f:ajax render="@form" execute="@form" /> </h:commandButton> <h:commandButton action="#{mBDocumentMigration.actionZapisz}" value="Zapisz" rendered="#{mBDocumentMigration.editingObject != null}" > <f:ajax render="@form" execute="@this" /> </h:commandButton> </p:column>
仔细看堆栈跟踪。从底部开始跟踪调用堆栈:
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
因此,它在渲染响应阶段发生。好的,看下一行(上面一行):
at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:123)
嘿,它已经通过Mojarra的内置的ajax异常处理程序AjaxExceptionHandlerImpl!仅在ajax请求期间发生异常时才调用此方法。好的,从下到上阅读下一行:
at com.sun.faces.renderkit.html_basic.HtmlResponseWriter.startCDATA(HtmlResponseWriter.java:630) at javax.faces.context.ResponseWriterWrapper.startCDATA(ResponseWriterWrapper.java:172) at javax.faces.context.PartialResponseWriter.startError(PartialResponseWriter.java:342) at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:210) at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)
因此,尝试将错误信息写入ajax响应。此信息必须在CDATA块中。然而,启动CDATA块失败如下,因为显然已经有一个CDATA块打开了:
java.lang.IllegalStateException: CDATA tags may not nest
这又表示在编写ajax响应时发生异常,很可能是因为您在生成HTML输出时仅调用getter方法执行业务逻辑。所以这个过程最有可能如下:
> JSF进入RENDER_RESPONSE阶段。
> JSF需要生成HTML输出。
>对于每个< f:ajax render =“some”> (或< p:ajax update =“some”>)),则需要创建< update id =“some”>在CDATA block之间生成的HTML输出的XML块(以保持XML输出在语法上有效)。所以需要启动一个CDATA块。
>将HTML输出生成到CDATA块中时,会对所有渲染时EL表达式进行求值,包括所有UI组件的value属性。
>在某个地方,EL表达式后面的getter会抛出一个由你自己的代码中的错误引起的异常。
> JSF会立即停止生成HTML输出,并没有关闭CDATA块。 HTTP响应包含半记录数据。
> AjaxExceptionHandlerImpl被触发。
> AjaxExceptionHandlerImpl需要将异常/错误详细信息写入响应。但是,它没有检查响应是否已经被写入。它盲目地尝试打开一个CDATA块,由此它已经被打开了。它抛出了你看到的异常,隐藏了所有关于它试图处理的真正的底层异常的细节。
你可以看到,问题是双重的:
> JSF渲染器不应该让响应halfbaked。
> Mojarra的AjaxExceptionHandlerImpl应该已经检查/验证了响应的状态。
如果您用custom one which immediately prints the stack trace或OmniFaces FullAjaxExceptionHandler
which is capable of detecting and cleaning halfbaked ajax responses替换Mojarra的内置ajax异常处理程序,那么最终会显示和显示代码中的错误引起的真正原因。如前所述,这很可能是performing business logic in a getter method,which is a bad practice造成的。
android – java.lang.IllegalStateException:找不到方法(View)
我有一个简单的应用程序,在三个屏幕的每一个上都有一个按钮.
前两个按钮应该将您从一个意图发送到下一个意图,直到最后一个应该拍照并在第三个屏幕上打印的按钮.
但是当我添加相机功能时,应用程序停止工作,现在当我按下第一个“开始spil”按钮时它甚至不会移动到第二个意图.
HomeActivity
package com.example.daniel.proto;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBaractivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class Home extends ActionBaractivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
public void onClick(View view)
{
Intent i = new Intent(this, SelectLevel.class);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onoptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onoptionsItemSelected(item);
}
}
SelectLevelActivity
package com.example.daniel.proto;
import android.support.v7.app.ActionBaractivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
public class SelectLevel extends ActionBaractivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_level);
}
public void onClick(View view)
{
Intent ii = new Intent (this, QRscanner.class);
startActivity(ii);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_select_level, menu);
return true;
}
@Override
public boolean onoptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onoptionsItemSelected(item);
}
}
QRscannerActivity
package com.example.daniel.proto;
import android.support.v7.app.ActionBaractivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.content.pm.PackageInfo;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class QRscanner extends ActionBaractivity
{
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView photoImageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
Button photoButton = (Button) findViewById(R.id.photoButton);
photoImageView = (ImageView) findViewById(R.id.photoImageView);
//disable the button if the user doesn't have a camera
if(!hasCamera())
photoButton.setEnabled(false);
}
//Check if the user has a camera
private boolean hasCamera()
{
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
//Launching the camera
public void launchCamera(View view)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Take a picture and pass results along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
//If you want to return the image taken
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
photoImageView.setimageBitmap(photo);
}
}
}
家庭XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Home"
android:background="#009900">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Spil"
android:id="@+id/playButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick=""
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="QuesteX"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:textSize="40dp" />
</RelativeLayout>
SelectLevel XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.daniel.proto.SelectLevel"
android:background="#72231F">
TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Udfordringer"
android:id="@+id/titleText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FFF" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/homeButton"
android:onClick="onClick"
android:layout_below="@+id/titleText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:layout_marginTop="49dp"
android:text="Click me" />
</RelativeLayout>
QRscanner XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.daniel.proto.QRscanner"
android:background="#990099">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo"
android:id="@+id/photoButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="65dp"
android:onClick="launchCamera" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoImageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:minHeight="300dp"
android:minWidth="300dp" />
</RelativeLayout>
logcat的
05-15 19:41:01.902 2927-2927/com.example.daniel.proto E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.proto, PID: 2927
java.lang.IllegalStateException: Could not find a method (View) in the activity class com.example.daniel.proto.Home for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'playButton'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: [class android.view.View]
at java.lang.class.getmethod(Class.java:664)
at java.lang.class.getmethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
解决方法:
你不小心从android内部删除了方法:onClick按钮.
更改:
android:onClick=""
至:
android:onClick="onClick"
android – java.lang.IllegalStateException:没有活动
我正在构建一个Android应用程序,我希望我的第一个活动由2个选项卡组成,一个用于用户的配置文件,另一个用于朋友的活动.对于这些标签,我选择了TabHost,因为我的Sherlock Action Bar已经使用列表导航移动到其他活动,因此我无法使用操作栏中的标签导航.
该应用程序工作了一段时间,但现在只有我的一个选项卡工作,一旦我尝试移动到第二个选项卡,我得到一个java.lang.IllegalStateException:没有活动
我为这个错误添加了Logcat输出,虽然我不确定它是否有帮助,因为我的代码没有回溯.
我尝试用FragmentTabHost替换TabHost,但错误仍然存在……
任何人都可以向我指出这个错误的起源/含义吗?提前致谢 !
LOGCAT:
04-03 08:19:39.380: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 08:19:39.400: E/AndroidRuntime(958): FATAL EXCEPTION: main
04-03 08:19:39.400: E/AndroidRuntime(958): java.lang.IllegalStateException: No activity
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1075)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1070)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:931)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1088)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Handler.handleCallback(Handler.java:725)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.os.Looper.loop(Looper.java:137)
04-03 08:19:39.400: E/AndroidRuntime(958): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 08:19:39.400: E/AndroidRuntime(958): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 08:19:39.400: E/AndroidRuntime(958): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 08:19:39.400: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-03 08:19:39.400: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-03 08:19:39.400: E/AndroidRuntime(958): at dalvik.system.NativeStart.main(Native Method)
活动
public class HomeActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener {
private ActionBar mActionBar;
private TabHost tHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity_layout);
//Setting the list navigation on actionBar
mActionBar = getSupportActionBar();
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromresource(context, R.array.list_menu_items, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
mActionBar.setdisplayShowTitleEnabled(false);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(list, this);
//Setting the subnavigation with TabHost
tHost = (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
/** Defining tab builder for profile tab */
TabHost.TabSpec tabSpecProfile = tHost.newTabSpec("profile");
tabSpecProfile.setIndicator("PROFILE");
tabSpecProfile.setContent(new TabContentMaker(this));
tHost.addTab(tabSpecProfile);
/** Defining tab builder for community tab */
TabHost.TabSpec tabSpecCommunity = tHost.newTabSpec("community");
tabSpecCommunity.setIndicator("COMmunitY");
tabSpecCommunity.setContent(new TabContentMaker(this));
tHost.addTab(tabSpecCommunity);
/** Defining Tab Change Listener event. This is invoked when tab is changed */
TabHost.OnTabchangelistener tabchangelistener = getonTabchangelistener();
/** Setting tabchangelistener for the tab */
tHost.setonTabChangedListener(tabchangelistener);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
//changing activity here
return true;
}
private TabHost.OnTabchangelistener getonTabchangelistener(){
TabHost.OnTabchangelistener tabchangelistener = new TabHost.OnTabchangelistener() {
@Override
public void onTabChanged(String tabId) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
MyProfileFragment profileFragment = (MyProfileFragment) fm.findFragmentByTag("profile");
CommunityFeedFragment communityFragment = (CommunityFeedFragment) fm.findFragmentByTag("community");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the profileFragment if exists */
if(profileFragment!=null)
ft.detach(profileFragment);
/** Detaches the communityFragment if exists */
if(communityFragment!=null)
ft.detach(communityFragment);
/** If current tab is profile */
if(tabId.equalsIgnoreCase("profile")){
if(profileFragment==null){
/** Create MyProfileFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new MyProfileFragment(), "profile");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(profileFragment);
}
}else{ /** If current tab is community */
if(communityFragment==null){
/** Create CommunityFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new CommunityFeedFragment(), "community");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(communityFragment);
}
}
ft.commit();
}
};
return tabchangelistener;
}
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enlavertical"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Styled" >
<activity
android:name="com.enlavertical.HomeActivity"
android:label="@string/app_name"
android:screenorientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
解决方法:
最后发现了这是什么:“无活动”崩溃是由于我使用错误的FragmentManager来嵌套Fragments.
对于嵌套片段,唯一有效的FragmentManager是通过调用getChildFragmentManager()
在包含Fragment中获得的片段.
android – java.lang.IllegalStateException:活动已被破坏
使用Robolectric,我对android很新.我使用Activity制作了第一个测试类.它工作得很好.
现在我想对片段进行测试.
@RunWith(RobolectricTestRunner.class)
public class LoginFragmentTest {
private LoginFragment fragment;
@Before
public void setup() {
fragment = new LoginFragment();
startFragment(fragment);
assertthat(fragment, notNullValue());
assertthat(fragment.getActivity(), notNullValue());
}
private void startFragment(LoginFragment fragment) {
FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragment, null);
fragmentTransaction.commit();
}
@Test
public void login() {
EditText idEditText = (EditText)fragment.getActivity().findViewById(R.id.main_id);
assertthat(idEditText, notNullValue());
}
}
这是我第一个Fragment类的测试类.它抛出
"java.lang.IllegalStateException: Activity has been destroyed" in startFragment#fragmentTransaction.commit().
谁知道如何解决这个问题?
你可以从https://github.com/msbaek/frame-test找到整个来源
提前致谢 !!
解决方法:
片段应该从Activity中显示.流程应该是:
>在FragmentActivity类中分配新的片段对象
>让片段管理器添加新分配的片段
在您的情况下,您没有与真实活动的连接.您使用新的FragmentActivity()分配FragmentActivity并尝试获取支持管理器.虽然这可以编译,但没有“真正的”活动能够管理您的片段.可以在已经显示的活动上添加片段,但事实并非如此.
我建议阅读这个页面,因为它很好地解释了这些:http://developer.android.com/guide/components/fragments.html
我们今天的关于java.lang.IllegalStateException:找不到线程绑定请求,方面是异常和java中错误:找不到或无法加载主类的分享就到这里,谢谢您的阅读,如果想了解更多关于ajax – java.lang.IllegalStateException:CDATA标签可能不嵌套、android – java.lang.IllegalStateException:找不到方法(View)、android – java.lang.IllegalStateException:没有活动、android – java.lang.IllegalStateException:活动已被破坏的相关信息,可以在本站进行搜索。
本文标签: