必须调用带有上下文和fragmentmanager的setup() - 渲染问题

时间:2017-04-02 17:22:51

标签: java android

我正在尝试使用android studio上的“Design”选项更改我的布局样式。 我得到一个错误,说:

渲染期间引发的异常:必须调用带有Context和FragmentManager(详细信息)的setup()

我无法理解导致此问题发生的原因,因为当我运行我的应用时,我可以正确看到布局,但无法对其进行编辑。

我的班级和布局是:

<?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 个答案:

答案 0 :(得分:1)

发生此渲染错误是因为android studio布局渲染器仍使用不推荐使用的setup()方法。关于这个here实际上是一个未解决的问题 如果设置正确,那么在运行应用程序时它将按预期工作。唯一的缺点是,在Android团队发布修复程序之前,你不会在编辑器上看到渲染的视图。