有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java活动不是Android Studio中的封闭类

我是Android新手。我已经用安卓创建了一个标签。支持v4。看法ViewPager。当我尝试使用Json请求从服务器获取数据时,它会给出错误消息

AccountActivity不是封闭类

如果您有任何帮助,我们将不胜感激

表1。爪哇

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;
    }


}

会计活动。爪哇

 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) 个答案

  1. # 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;
    }