2017-04-02 24 views
0

我試圖更改使用「設計」在Android工作室的選擇我的佈局風格。 我得到一個錯誤,說:必須調用設置(),它的上下文和fragmentmanager - 渲染問題

異常渲染期間提出:必須呼叫建立(),需要一個語境和FragmentManager(詳細信息)

我不明白是什麼導致這個問題出現,因爲當我跑我的應用程序,我可以看到正確的佈局,但不能編輯它。

我的班級和佈局:

<?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" 
    android:weightSum="1" 
    android:baselineAligned="false" 
    android:orientation="vertical" 
    tools:layout_editor_absoluteY="81dp" 
    tools:layout_editor_absoluteX="0dp"> 


    <TextView 
     android:id="@+id/item_page_product_name" 
     android:layout_width="368dp" 
     android:layout_height="wrap_content" 
     android:text="@string/nike_show" 
     android:textSize="18sp" 
     android:background="@null" 
     tools:layout_editor_absoluteX="8dp" 
     android:padding="3dp" 
     android:paddingStart="8dp" 
     tools:ignore="MissingConstraints,RtlSymmetry" 
     tools:layout_editor_absoluteY="8dp" /> 

    <TextView 
     android:text="@string/item_page_item_des" 
     android:layout_width="326dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/item_page_product_des" 
     tools:ignore="MissingConstraints" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/item_page_product_name" 
     android:layout_marginStart="1dp" 
     app:layout_constraintLeft_toRightOf="@+id/item_page_item_id" /> 

    <TextView 
     android:text="@string/item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="8dp" 
     android:id="@+id/item_page_item_id" 
     tools:ignore="MissingConstraints" 
     android:layout_marginTop="8dp" 
     android:paddingStart="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/item_page_product_name" 
     android:paddingRight="3dp" 
     android:paddingLeft="3dp" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/item_page_pager" 
     android:layout_width="368dp" 
     android:layout_height="213dp" 
     tools:layout_editor_absoluteX="8dp" 
     android:layout_marginTop="4dp" 
     app:layout_constraintTop_toBottomOf="@+id/item_page_product_des" 
     tools:ignore="MissingConstraints" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@+id/tabsHostItem" 
     android:layout_width="368dp" 
     android:layout_height="325dp" 
     tools:layout_editor_absoluteX="8dp" 
     tools:ignore="MissingConstraints" 
     app:layout_constraintTop_toBottomOf="@+id/item_page_pager"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <FrameLayout 
        android:id="@+id/tab" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:orientation="vertical" 
        tools:ignore="UselessParent"> 
        <!--<TextView--> 
        <!--android:layout_width="match_parent"--> 
        <!--android:layout_height="match_parent"--> 
        <!--android:text="@string/this_is_tab_1" />--> 
       </FrameLayout> 

       <!--<LinearLayout--> 
       <!--android:id="@+id/tab2"--> 
       <!--android:layout_width="match_parent"--> 
       <!--android:layout_height="match_parent"--> 
       <!--android:orientation="vertical">--> 
       <!--<TextView--> 
       <!--android:layout_width="match_parent"--> 
       <!--android:layout_height="match_parent"--> 
       <!--android:text="@string/this_is_tab_2" />--> 

       <!--</LinearLayout>--> 

       <!--<LinearLayout--> 
       <!--android:id="@+id/tab3"--> 
       <!--android:layout_width="match_parent"--> 
       <!--android:layout_height="match_parent"--> 
       <!--android:orientation="vertical">--> 
       <!--<TextView--> 
       <!--android:layout_width="match_parent"--> 
       <!--android:layout_height="match_parent"--> 
       <!--android:text="@string/this_is_tab_3" />--> 

       <!--</LinearLayout>--> 
      </FrameLayout> 
     </LinearLayout> 

    </android.support.v4.app.FragmentTabHost> 

</android.support.constraint.ConstraintLayout> 

類:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.FragmentTabHost; 
import android.support.v4.app.NavUtils; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.HorizontalScrollView; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.goldbergtom.cricket.adapters.ViewPagerAdapter; 
import com.goldbergtom.cricket.fragments.DescriptionFragment; 

import controller.ControllerDao; 

public class ItemPageActivity extends AppCompatActivity { 


    private android.support.v4.app.FragmentTabHost mTabHost; 

    ViewPager viewPager; 
    PagerAdapter adapter; 
    int[] flag; 

    private LinearLayout mGallery; 
    private int[] mImgIds; 
    private LayoutInflater mInflater; 
    private HorizontalScrollView horizontalScrollView; 

    private ImageButton back; 
    private ImageButton www; 
    private ImageButton settings; 

    private TextView productMainTitle; 
    private TextView productId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_item_page); 
     mInflater = LayoutInflater.from(this); 


     productMainTitle = (TextView)findViewById(R.id.item_page_product_name); 
     productId = (TextView)findViewById(R.id.item_page_product_des); 
     // productMainTitle = (TextView)findViewById(R.id.item_page_product_name); 
     // productMainTitle = (TextView)findViewById(R.id.item_page_product_name); 


     String pMT = ControllerDao.currentSessionUser.getProducts()[0].get_title(); 
     String pid = ControllerDao.currentSessionUser.getProducts()[0].getId(); 

     productMainTitle.setText(pMT); 
     productId.setText(pid); 




     // TODO : yahav - remove the comments on the following code and look what it does 

//  android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
//  actionBar.setDisplayShowTitleEnabled(false); 
//  actionBar.setDisplayShowHomeEnabled(false); 
// 
//  View mCustomView = mInflater.inflate(R.layout.cricket_action_bar, null); 
// 
//  actionBar.setCustomView(mCustomView); 
//  actionBar.setDisplayShowCustomEnabled(true); 
// 
//  back = (ImageButton)findViewById(R.id.toolbar_back); 
//  back.setOnClickListener(backButtonFromItemActivity); 
// 
//  www = (ImageButton)findViewById(R.id.toolbar_www); 
//  www.setOnClickListener(wwwButtonFromItemActivity); 
// 
//  settings = (ImageButton)findViewById(R.id.toolbar_settings1); 
//  settings.setOnClickListener(settingsButtonFromItemActivity); 
// 
     flag = new int[] { R.drawable.ic_1 , R.drawable.ic_2 , R.drawable.ic_3 }; 

     // Locate the ViewPager in viewpager_main.xml 
     viewPager = (ViewPager) findViewById(R.id.item_page_pager); 
     // Pass results to ViewPagerAdapter Class 
     adapter = new ViewPagerAdapter(ItemPageActivity.this, flag); 
     // Binds the Adapter to the ViewPager 
     viewPager.setAdapter(adapter); 

     mTabHost = (FragmentTabHost)findViewById(R.id.tabsHostItem); 

//  FragmentManager fm = getFragmentManager(); 
//  android.app.FragmentTransaction ft = fm.beginTransaction(); 
//  ft.commit(); 

     mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

     mTabHost.addTab(
       mTabHost.newTabSpec("tab1").setIndicator("DESCRIPTION",null),DescriptionFragment.class,null); 

     mTabHost.addTab(
       mTabHost.newTabSpec("tab2").setIndicator("HISTORY",null),DescriptionFragment.class,null); 

     mTabHost.addTab(
       mTabHost.newTabSpec("tab3").setIndicator("ALERTS",null),DescriptionFragment.class,null); 

     //Tab 1 
//  TabHost.TabSpec spec = host.newTabSpec("DESCRIPTION"); 
//  spec.setContent(R.id.tab1); 
//  spec.setIndicator("Tab One"); 
//  host.addTab(spec); 
// 
//  //Tab 2 
//  spec = host.newTabSpec("HISTORY"); 
//  //spec.setContent(R.layout.fragment_description); 
//  spec.setContent(R.id.tab2); 
//  spec.setIndicator("Tab Two"); 
//  host.addTab(spec); 
// 
//  //Tab 3 
//  spec = host.newTabSpec("ALERTS"); 
//  spec.setContent(R.id.tab3); 
//  spec.setIndicator("Tab Three"); 
//  host.addTab(spec); 

     // CricketDao.GetDataTask gdt = new CricketDao.GetDataTask(); 
//  String s = null; 
//  JSONObject jObject = null, subject = null; 
//  JSONArray arr = null; 
//  String title=null; 

//  try { 
//   // TODO : Place resonse as USER object to const 
//   s = gdt.execute("https://cricketweb.herokuapp.com/checkLogin").get(); 
//  } catch (InterruptedException e) { 
//   e.printStackTrace(); 
//  } catch (ExecutionException e) { 
//   e.printStackTrace(); 
//  } 
    } 

    public boolean onCreateOptionsMenu (Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_item_activity, menu); 
     return true; 
    } 

    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. 
     switch (item.getItemId()){ 

      // when clicking on the URL icon, it will open the product page through browser. 
      case R.id.action_web: 
       startActivity(new Intent(this, LoginActivity.class)); 
       return true; 

      // when clicking on delete item it will delete the item from wish list and go back to wish list page. 
      case R.id.delete_item: 
       startActivity(new Intent(this, WishListActivity.class)); 
       return true; 

      // when clicking on purchased item, it will stay on this activity and change on DB from 0 to 1 (help us with adaptation) 
      case R.id.purchased_item: 
       startActivity(new Intent(this, LoginActivity.class)); 
       return true; 

      // when clicking on settings, it will give the user another view of settings option. 
      case R.id.action_settings: 
       startActivity(new Intent(this, LoginActivity.class)); 
       return true; 

      // when clicking on back icon it will go back to previous screen. 
      case R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 
       return true; 

     } 

     return super.onOptionsItemSelected(item); 
    } 


    View.OnClickListener backButtonFromItemActivity = new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "backButtonFromItemActivity", Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    View.OnClickListener wwwButtonFromItemActivity = new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "wwwButtonFromItemActivity", Toast.LENGTH_SHORT).show(); 
     } 
    }; 
    View.OnClickListener settingsButtonFromItemActivity = new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "settingsButtonFromItemActivity", Toast.LENGTH_SHORT).show(); 
     } 
    }; 
    private void initData() 
    { 

     mImgIds = new int[] { R.drawable.ic_1, R.drawable.ic_2, R.drawable.ic_3,R.drawable.ic_4 
     }; 
    } 

    public void toggle_contents(View v){ 
     new DescriptionFragment().toggle_contents(v); 
    } 

    public void toggle_contents2(View view) { 
     new DescriptionFragment().toggle_contents2(view); 
    } 

} 

回答

1

會出現此渲染誤差,因爲Android的工作室佈局渲染器仍然使用過時的設置()方法。實際上,有一個開放的問題,關於這個here 如果設置正確像你這樣當你運行應用程序預期它會奏效。唯一的缺點是,你將不會看到在編輯器,直到Android團隊職位修復此渲染視圖。

+1

此問題現在應該修復https://issuetracker.google.com/issues/37128407 –

+0

FWIW,它似乎並不適合我。我用搖籃2.3.3/2.3.3 AS,而且我也得到它的預覽。我打電話的設置(上下文,FragmentManager,INT)。 – Oded