1

我想解析我的 xml 并在我的 pagetitles 上显示我解析的字符串。但我得到一个 nullponitereexception。

这是我的飞溅活动:

package de.heron.xmlpullparser;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import de.heron.R;
import de.heron.SettingsActivity;
import de.heron.model.RoomReader;
import de.heron.parser.RoomParser;

public class SplashActivity extends FragmentActivity{
    private RoomParser mNewsFeeder;
    private List<RoomReader> mRssFeedList;
    private static final String TOPSTORIES =
            "URL";
    public static String[] TabTitles = {RoomReader.getName()};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_striptabs);
        mRssFeedList = new ArrayList<RoomReader>();
        new DoRssFeedTask().execute(TOPSTORIES);    
    }

    static class RssHolder {
        //textview textview1; usw...

    }

    public class DoRssFeedTask extends AsyncTask<String, Void, List<RoomReader>> {
        ProgressDialog prog;
        String jsonStr = null;
        Handler innerHandler;

        @Override
        protected void onPreExecute() {
            //Context context = getContext();
            prog = new ProgressDialog(SplashActivity.this);
            prog.setMessage("Loading....");
            prog.show();
        }

        @Override
        protected List<RoomReader> doInBackground(String... params) {
            for (String urlVal : params) {
                mNewsFeeder = new RoomParser(urlVal);
            }
            mRssFeedList = mNewsFeeder.parse();
            return mRssFeedList;
        }

        @Override
        protected void onPostExecute(List<RoomReader> result) {
            prog.dismiss();
            String key = "key";
             Bundle b=new Bundle();
             b.putStringArray(key, new String[]{RoomReader.getName()});
             Intent i=new Intent(SplashActivity.this, MainActivity.class);
             i.putExtras(b);
             SplashActivity.this.startActivity(i);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }  
  }

这是我的片段适配器:

    public class MyPagerAdapter extends FragmentPagerAdapter{
String key = "key";
        Bundle b = MainActivity.this.getIntent().getExtras();
    //public final String[] TITLES =  b.getStringArray(key);
        public final String[] TITLES = SplashActivity.TabTitles;
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return SplashActivity.TabTitles[position];//TITLES[position];

             }
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new Fragment1();
            Bundle args = new Bundle();
            args.putInt(Fragment1.position, position + 1);
            fragment.setArguments(args);
            return fragment;
        }
        /*@Override  
        public Fragment getItem(int position) {  
            Fragment fragment = new Fragment();  
            switch (position) {  
            case 0:  
                return fragment = new Fragment1();  
            case 1:  
                return fragment = new Fragment2();
            default:  
                break;  
            }  
            return fragment;  
    }*/

        @Override
        public int getCount() {
            return TITLES.length;
        }
    }

这是我的 mainactivty 中的异步任务:

 private class RssAdapter extends ArrayAdapter<RoomReader> {
        public RssAdapter(Context context, int textViewResourceId, List<RoomReader> rssFeedLst) {
            super(context, textViewResourceId, rssFeedLst);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = convertView;
            RssHolder rssHolder = null;
            if (convertView == null) {
                //textview id setzten usw..
                view.setTag(rssHolder);
            } else {
                rssHolder = (RssHolder) view.getTag();
                //textview set text RoomReader.getName() usw...
            }

            return view;
        }
    }

    static class RssHolder {
        //textview textview1; usw...

    }

    public class DoRssFeedTask extends AsyncTask<String, Void, List<RoomReader>> {
        ProgressDialog prog;
        String jsonStr = null;
        Handler innerHandler;

        @Override
        protected void onPreExecute() {
            //Context context = getContext();
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();
        }

        @Override
        protected List<RoomReader> doInBackground(String... params) {
            for (String urlVal : params) {
                mNewsFeeder = new RoomParser(urlVal);
            }
            mRssFeedList = mNewsFeeder.parse();
            return mRssFeedList;
        }

        @Override
        protected void onPostExecute(List<RoomReader> result) {
            prog.dismiss();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
}

我的主要活动中的 oncreate:

 private ListView mRssListView;
    private RoomParser mNewsFeeder;
    private List<RoomReader> mRssFeedList;
    private RssAdapter mRssAdap;
    private static final String TOPSTORIES =
            "the url";
    private PagerSlidingTabStrip tabs;
    private ViewPager pager;
    private MyPagerAdapter adapter;
     private static TextToSpeech tts;
        private static final int REQUEST_CODE = 1234;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_striptabs);
        tts = new TextToSpeech(this, this);
        mRssFeedList = new ArrayList<RoomReader>();
        new DoRssFeedTask().execute(TOPSTORIES);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        tabs.setViewPager(pager);
    }   

日志:

08-12 18:06:00.110: E/AndroidRuntime(21128): FATAL EXCEPTION: main
08-12 18:06:00.110: E/AndroidRuntime(21128): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.heron/de.heron.xmlpullparser.MainActivity}: java.lang.NullPointerException
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.os.Looper.loop(Looper.java:137)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at java.lang.reflect.Method.invokeNative(Native Method)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at java.lang.reflect.Method.invoke(Method.java:525)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at dalvik.system.NativeStart.main(Native Method)
08-12 18:06:00.110: E/AndroidRuntime(21128): Caused by: java.lang.NullPointerException
08-12 18:06:00.110: E/AndroidRuntime(21128):    at com.astuetz.viewpager.extensions.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at com.astuetz.viewpager.extensions.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:182)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at de.heron.xmlpullparser.MainActivity.onCreate(MainActivity.java:72)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.Activity.performCreate(Activity.java:5133)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-12 18:06:00.110: E/AndroidRuntime(21128):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-12 18:06:00.110: E/AndroidRuntime(21128):    ... 11 more
4

1 回答 1

0

我建议在打开 MainActivity 之前解析 URL。例如,您可以创建一个 Splashscreen 活动并在那里编写您的 asyncTask 以解析 URL,然后在您的 splashScreen 活动中的 asyncTask 的 onPostExecute() 方法上,您可以创建 MainActivity 的意图。您可以使用意图方法(intent.putExtra())或简单地设置一些静态变量将解析的字符串传递给 MainActivity。

    public class SplashScreen extends Activity {


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


            new DataFromServer().execute();

        }

        private class DataFromServer extends AsyncTask<Void, String, Void> {

            ProgressDialog progressDialog = null;

            @Override
            protected void onPreExecute() {
            // TODO Auto-generated method stub
            //super.onPreExecute();



            progressDialog = ProgressDialog.show(SplashScreen.this,
                "Loading...",
                "Please wait while Ads are being loaded", true,
                false);

           }

           @Override
           protected Void doInBackground(Void... params) {

            /* fetch the data from server here and set the titles variables of the
                           MainActivity */
            // e.g 
            MainActivity.tab1_title = some_data_from_server;
            MainActivity.tab2_title = some_data_from_server;
            MainActivity.tab3_title = some_data_from_server;

        return null;
          }


         @Override
         protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            Log.e("tab1: ",MainActivity.tab1_title);
                        Log.e("tab2: ",MainActivity.tab2_title);
                        Log.e("tab3: ",MainActivity.tab3_title);
            Intent intent = new Intent(SplashScreen.this,MainActivity.Class);
            StartActivity(intent);
            SplashScreen.this.finish();

            if (progressDialog != null) { 
            progressDialog.dismiss();
        }
    }

}

}

public class MainActivity extends Activity {

public static String tab1_title;
public static String tab2_title;
public static String tab3_title;

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

    // Now you can use the tab1_title, tab2_title and tab3_title here
}

}

于 2013-08-12T14:14:27.290 回答