我有一個活動MainActivity.java,它將字符串值發送到另一個活動ViewFragActivity.java中,我必須實現4個片段。在每個片段中,我必須使用jsoup相應地使用從前一活動收到的字符串來獲取數據。我得到一個空指針異常,這是我的問題是..而不是片段我使用活動jsoup正常工作。我認爲問題是在片段中執行asynctask。有人?Jsoup Asynctask實現具有以前活動的值的片段
ViewFrag.java
public class ViewFragActivity extends FragmentActivity implements TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Movies", "Games", "Fun", "Events" };
Bundle extras;
public String key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_frag);
Intent intent = getIntent();
key = intent.getStringExtra("String");
Toast.makeText(getApplicationContext(), key, Toast.LENGTH_SHORT).show();
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Fragment1.java
public class Fragment1 extends Fragment {
TextView respText, date;
String url1 = "";
.....
String url7 = "";//Each containing URLs
.....
String url12 = "";
ProgressDialog mProgressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_daily, container,
false);
init();
return rootView;
}
private void init() {
respText = (TextView) getView().findViewById(R.id.site_name_2);
date = (TextView) getView().findViewById(R.id.date);
String key = ((ViewFragActivity) this.getActivity()).key;
switch (key) {
case "A":
new Test(getActivity()).execute(url1);
break;
case "B":
new Test(getActivity()).execute(url2);
break;
case "C":
new Test(getActivity()).execute(url3);
break;
......//more cases
}
}
public class Test extends AsyncTask<String, Void, Void> {
String desc;
String dat;
Activity mContex;
public Test(Activity context)
{
this.mContex=context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = ProgressDialog.show(getActivity(),"",null, true);
mProgressDialog.setTitle("Astro");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
String url = params[0];
try {
Document document = Jsoup.connect(url).get();
Elements myin = document.select("div#monday");
org.jsoup.nodes.Element firstdate = myin.select("p:eq(0)")
.first();
org.jsoup.nodes.Element secondParagraph = firstdate
.nextElementSibling();
desc = secondParagraph.text();
dat = firstdate.text();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
respText.setText(desc);
date.setText(dat);
mProgressDialog.dismiss();
}
}
}
日誌
10-04 12:04:38.847: E/AndroidRuntime(23411): FATAL EXCEPTION: main
10-04 12:04:38.847: E/AndroidRuntime(23411): Process: com.chiron.astrotest, PID: 23411
10-04 12:04:38.847: E/AndroidRuntime(23411): java.lang.NullPointerException
10-04 12:04:38.847: E/AndroidRuntime(23411): at com.chiron.astrotest.Fragment1.init(Fragment1.java:58)
10-04 12:04:38.847: E/AndroidRuntime(23411): at com.chiron.astrotest.Fragment1.onCreateView(Fragment1.java:53)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.View.measure(View.java:16497)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.View.measure(View.java:16497)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-04 12:04:38.847: E/AndroidRuntime(23411): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.View.measure(View.java:16497)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-04 12:04:38.847: E/AndroidRuntime(23411): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2553)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.View.measure(View.java:16497)
10-04 12:04:38.847: E/AndroidRuntime(23411): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
UPDATE:新logc在
10-04 12:38:35.994: E/AndroidRuntime(30527): FATAL EXCEPTION: main
10-04 12:38:35.994: E/AndroidRuntime(30527): Process: com.chiron.astrotest, PID: 30527
10-04 12:38:35.994: E/AndroidRuntime(30527): java.lang.NullPointerException
10-04 12:38:35.994: E/AndroidRuntime(30527): at com.chiron.astrotest.Fragment1$Test.onPostExecute(Fragment1.java:145)
10-04 12:38:35.994: E/AndroidRuntime(30527): at com.chiron.astrotest.Fragment1$Test.onPostExecute(Fragment1.java:1)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.os.AsyncTask.finish(AsyncTask.java:632)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.os.Handler.dispatchMessage(Handler.java:102)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.os.Looper.loop(Looper.java:136)
10-04 12:38:35.994: E/AndroidRuntime(30527): at android.app.ActivityThread.main(ActivityThread.java:5081)
10-04 12:38:35.994: E/AndroidRuntime(30527): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 12:38:35.994: E/AndroidRuntime(30527): at java.lang.reflect.Method.invoke(Method.java:515)
10-04 12:38:35.994: E/AndroidRuntime(30527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-04 12:38:35.994: E/AndroidRuntime(30527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-04 12:38:35.994: E/AndroidRuntime(30527): at dalvik.system.NativeStart.main(Native Method)
UPDATE工作守則
public class Fragment1 extends Fragment {
TextView respText, date;
String url1 = "";
.....
String url7 = "";//Each containing URLs
.....
String url12 = "";
ProgressDialog mProgressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_daily, container,
false);
respText = (TextView) rootView.findViewById(R.id.site_name_2);//CHANGE
date = (TextView) rootView.findViewById(R.id.date);//CHANGE
init();
return rootView;
}
private void init() {
respText = (TextView) getView().findViewById(R.id.site_name_2);
date = (TextView) getView().findViewById(R.id.date);
String key = ((ViewFragActivity) this.getActivity()).key;
switch (key) {
case "A":
new Test(getActivity()).execute(url1);
break;
case "B":
new Test(getActivity()).execute(url2);
break;
case "C":
new Test(getActivity()).execute(url3);
break;
......//more cases
}
}
public class Test extends AsyncTask<String, Void, Void> {
String desc;
String dat;
Activity mContex;
public Test(Activity context)
{
this.mContex=context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = ProgressDialog.show(getActivity(),"",null, true);
mProgressDialog.setTitle("Astro");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
String url = params[0];
try {
Document document = Jsoup.connect(url).get();
Elements myin = document.select("div#monday");
org.jsoup.nodes.Element firstdate = myin.select("p:eq(0)")
.first();
org.jsoup.nodes.Element secondParagraph = firstdate
.nextElementSibling();
desc = secondParagraph.text().toString();//CHANGE
dat = firstdate.text().toString();//CHANGE
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
respText.setText(desc);
date.setText(dat);
mProgressDialog.dismiss();
}
}
}
顯示一行文字Fragment1.java:58 line – prakash 2014-10-04 06:53:01
line 57 private void init(){line 58 respText =(TextView)getView()。findViewById(R.id.site_name_2); – 2014-10-04 07:02:56