Activity 不是 Android Studio 中的封闭类

Posted

技术标签:

【中文标题】Activity 不是 Android Studio 中的封闭类【英文标题】:Activity is not an enclosing class in Android Studio 【发布时间】:2016-01-15 00:19:44 【问题描述】:

我是 android 新手。我已经使用 android.support.v4.view.ViewPager 创建了选项卡。当我尝试使用 Json Request 从服务器获取数据时,它给了我错误

AccountActivity 不是封闭类

任何帮助将不胜感激

TAb1.java

public class Tab1 extends Fragment 
    private ProgressDialog pDialog;
    private  UserAddress userAddress;
    private TextView txtResponsec;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
        new LoadUserAddress().execute();

    
    class LoadUserAddress extends AsyncTask<String,String,String> 
        private ProgressDialog progressDialog;

        @Override
        protected String doInBackground(String... params) 
            try 
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                JSONHttpClient jsonHttpClient = new JSONHttpClient();
                userAddress = jsonHttpClient.Get(ServiceUrl.UserAddress, nameValuePairs, UserAddress.class);
                String id = userAddress.GetId();
            
            catch (Exception ex)
                String message = ex.getMessage();
            
            return  null;
        

        @Override
        protected void onPreExecute() 
            super.onPreExecute();    //To change body of overridden methods use File | Settings | File Templates.
            progressDialog = new ProgressDialog(AccountActivity.this);
            progressDialog.setMessage("Loading products. Please wait...");
            progressDialog.show();
        

        @Override
        protected void onPostExecute(String s) 
            progressDialog.dismiss();
            getActivity().runOnUiThread(new Runnable() 
                @Override
                public void run() 


                    txtResponsec.setText(userAddress.GetCountry());

                
            );
        
    

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

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



AccountActivity.java

 public class AccountActivity extends AppCompatActivity 



        Toolbar toolbar;
        ActionBar actionBar;
        ViewPager pager;
        ViewPagerAdapter adapter;
        SlidingTabLayout tabs;
        CharSequence Titles[]="Info.","Address","Contact","Email";
        int Numboftabs =4;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_account);

            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            assert getSupportActionBar() != null;
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            if (toolbar != null) 
                toolbar.setNavigationOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        onBackPressed();
                    
                );
            
            adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

            // Assigning ViewPager View and setting the adapter
            pager = (ViewPager) findViewById(R.id.pager);
            pager.setAdapter(adapter);

            // Assiging the Sliding Tab Layout View
            tabs = (SlidingTabLayout) findViewById(R.id.tabs);
            tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width


            // Setting Custom Color for the Scroll bar indicator of the Tab View
            tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() 
                @Override
                public int getIndicatorColor(int position) 
                    return getResources().getColor(R.color.tabsScrollColor);
                
            );

            // Setting the ViewPager For the SlidingTabsLayout
            tabs.setViewPager(pager);




        


        @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_account, 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);
        
    

【问题讨论】:

【参考方案1】:

你应该改变

 progressDialog = new ProgressDialog(AccountActivity.this);

 progressDialog = new ProgressDialog(getActivity());

Tab1 Fragment 我也看到txtResponsec=null 你忘了初始化它。

编辑:

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

    View v =inflater.inflate(R.layout.tab_1,container,false);
    txtResponsec=(TextView)v.findViewById(R.id.txtResponsecId);
    return v;

【讨论】:

以上是关于Activity 不是 Android Studio 中的封闭类的主要内容,如果未能解决你的问题,请参考以下文章

android新创建Activity是不是需要在Manifest文件中注册,如何进行注册

Android开发学习—— 创建项目时,不是继承activity,而是继承ActionBarActivity

android 判断activity是不是被回收

android新创建Activity是不是需要在Manifest文件中注册,如何进行注册?

如何在android中获取Fragment的包名(不是Activity的)[重复]

为啥fragmentactivity 不是每次都执行了oncreate