我制作的Android应用程序可以加载网页并将其转换为原生Android应用。我的计划是让导航抽屉有一个链接列表,这些链接会在点击时加载。问题是,当我从导航抽屉调用页面加载方法时,它传递的变量以某种方式设置为null。我知道变量不是空的,因为如果按钮调用相同的方法,它就可以完美地工作。有问题的变量是一个Map,当作为参数传递时,以及当我将其设置为public并直接访问它时,返回null。
网址加载类:
public class LoadPage implements Callable<Document> {
private String URL;
private Map<String, String> cookies;
public LoadPage(String URL, Map<String, String> cookies) {
this.URL = URL;
//cookies is null if this gets called from the navigation drawer
//MainActivity.cookies is also null when called from navigation drawer
this.cookies = new HashMap<>(cookies); //This throws a null pointer exceptions, as does putAll()
}
public Document call() {
try {
Document doc = Jsoup.connect(URL)
.cookies(cookies)
.get();
return doc;
} catch (IOException e) {
//handle exception
}
}
}
MainActivity.java的一部分,用于处理导航抽屉选择和页面加载
public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public static Map<String, String> cookies;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cookies = new HashMap<String, String>();
cookies.put("get information passed via putExtra from login Activity);
}
}
@Override
public void onNavigationDrawerItemSelected(int position) {
Log.e("something", "something");
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
loadPage(null);
}
//omitted code
public void loadPage(View v) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Document> request = executor.submit(new LoadPage("Example.com", cookies));
try {
Document page = request.get(5, TimeUnit.SECONDS);
Elements elements = page.select("criteria for item selection");
currentPage = page;
ArrayList<TopicLink> topics = new ArrayList<TopicLink>(elements.size());
mTitle = page.title();
restoreActionBar();
for (Element e : elements) {
//send data to adapter class for listview population
}
TopicAdapter adapter = new TopicAdapter(this, R.id.listview, topics);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
} catch (InterruptedException | ExecutionException e) {
} catch (TimeoutException e) {
} catch (Exception e) {
}
}
出于某种原因,loadPage在按下按钮时会很好,但是当通过导航抽屉调用时,cookies Map就会变为空。
编辑:logcat
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Cookie map must not be null
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.report(FutureTask.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.get(FutureTask.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.loadPage(MainActivity.java:248)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:136)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:245)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:98)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Fragment.performCreate(Fragment.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.addFragment(FragmentManager.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.onCreateView(Activity.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.rInflate(LayoutInflater.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(Native Method)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.setContentView(Activity.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.onCreate(MainActivity.java:77)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.performCreate(Activity.java)
06-20 20:22:34.783 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.os.Looper.loop(Looper.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ Caused by: java.lang.IllegalArgumentException: Cookie map must not be null
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at org.jsoup.helper.Validate.notNull(Validate.java:26)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at org.jsoup.helper.HttpConnection.cookies(HttpConnection.java:139)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.LoadPage.call(LoadPage.java:40)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.LoadPage.call(LoadPage.java:20)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
06-20 20:22:34.793 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.Thread.run(Thread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Cookie map must not be null
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.report(FutureTask.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.get(FutureTask.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.loadPageURL(MainActivity.java:318)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:142)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:245)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:98)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Fragment.performCreate(Fragment.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.FragmentManagerImpl.addFragment(FragmentManager.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.onCreateView(Activity.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.rInflate(LayoutInflater.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(Native Method)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.setContentView(Activity.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.MainActivity.onCreate(MainActivity.java:77)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Activity.performCreate(Activity.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.os.Looper.loop(Looper.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ Caused by: java.lang.IllegalArgumentException: Cookie map must not be null
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at org.jsoup.helper.Validate.notNull(Validate.java:26)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at org.jsoup.helper.HttpConnection.cookies(HttpConnection.java:139)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.LoadPage.call(LoadPage.java:40)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at com.HyperStandard.llr.app.LoadPage.call(LoadPage.java:20)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
06-20 20:22:34.803 22835-22835/com.HyperStandard.llr.app W/System.err﹕ at java.lang.Thread.run(Thread.java)
我尝试使用备用构造函数等来初始化Map但是它们都抛出了一些NullPointerException的变体