2017-07-01 140 views
1

我有一個ListView在ListView.class,當用戶點擊一個項目(假設位置= 0的項目)Fragment6應該打開。使用片段破壞的活動

此外,我有一個HandleListClick.class獲取位置(例如位置= 0),然後使用switch來打開一個片段,但我得到一個錯誤。

java.lang.IllegalStateException:活動遭到破壞

我該如何處理這個惱人的問題?我的活動在Manifest中聲明。

這是我ListView.class

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      // ListView Clicked item index 
      int itemPosition  = position; 

      // ListView Clicked item value 
      String itemValue = (String) listView.getItemAtPosition(position); 


       if (position==0){ 
        HandleListClick handleListClick = new HandleListClick(); 
        handleListClick.getItemPosition(0); 
       } 
} 

這裏是我的handleListClick.class

public class HandleListClick extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_handle_list_click); 
} 


public void getItemPosition(int position) { 
    switch (position) { 
     case 0: 
      Fragment6 frag6 = null; 
      frag6 = new Fragment6(); 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.framelayout, frag6) 
        .commit(); 


    } 
} 

}

這裏是我的Fragment6.java

public class Fragment6 extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.fragmentdescrip6, container,false); 
    return v; 



// ViewGroup rootView = (ViewGroup) inflater.inflate(
    //   R.layout.fragmentdescrip6, container, false); 

// return rootView; 
} 

}

這裏是activity_handle_list_click.xml

 <?xml version="1.0" encoding="utf-8"?> 
     <android.support.constraint.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.user.app.HandleListClick"> 


    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id= "@+id/framelayout" 
     > 


    </FrameLayout> 

</android.support.constraint.ConstraintLayout> 

這裏是fragmentdescrip6.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/frag6" 

    > 

    <TextView 
     android:id="@+id/textfrag6label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="foo" 
     android:textStyle="bold" 
     android:textColor="#FFFF4444" 
     android:textSize="20dp" 
     android:layout_marginTop="17dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/textfrag6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/textfrag6label" 
     android:layout_marginLeft="16dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="33dp" 
     android:textStyle="bold" 
     android:text="some Text" /> 


</RelativeLayout> 

回答

2

嘗試這種方式

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

       // ListView Clicked item index 
       int itemPosition  = position; 

       // ListView Clicked item value 
       String itemValue = (String) listView.getItemAtPosition(position); 


       if (position==0){ 
        Intent intent = new Intent(CurrentActivity.this, HandleListClick.class); 
        intent.putExtra("POSITION", position); 
        startActivity(intent); 
       } 
        /* HandleListClick handleListClick = new HandleListClick(); 
        handleListClick.getItemPosition(0);*/ 
       } 
      } 

更改活動代碼

public class HandleListClick extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_handle_list_click); 
      Intent intent = getIntent(); 
      int pos = intent.getIntExtra("POSITION",0); 
      getItemPosition(pos); 
     } 


     public void getItemPosition(int position) { 
      switch (position) { 
       case 0: 
        Fragment6 frag6 = null; 
        frag6 = new Fragment6(); 
        FragmentManager fragmentManager = getSupportFragmentManager(); 
        fragmentManager.beginTransaction() 
          .replace(R.id.framelayout, frag6) 
          .commit(); 


      } 
     } 
    } 
+0

謝謝!它終於奏效了。我非常感謝 – Blnpwr

+0

但是如果我的位置= 1,會發生什麼?然後'int pos = intent.getIntExtra(「POSITION」,0);'不起作用,對吧? – Blnpwr

+1

如果position == 1,那麼HandleListClick類的onCreate將不會被調用,因爲我們只在onItemClick()中放置了位置== 0的條件。 –