我正在使用ActionBarSherlock
和viewPagerIndicator
撰写应用程序。
My Aim is
:
当我从AddDialogue.java
选择菜单项时,启动对话活动(MainScrenActivity.java
)。通过按AddDialogue.java
中的按钮,它应检查网络连接并显示listview
中的项目。
在AddDialogue.java
中我使用的是updateListview()方法,该方法在testFragment类中定义。
AddDialogue.java:
public class AddDialogue extends SherlockFragmentActivity {
private static final int THEME = R.style.Theme_Sherlock_Light_Dialog;
private Button goButton;
private String strurl = "http://feeds.hindustantimes.com/HT-IndiaSectionPage-Topstories";
EditText editText;
TestFragment testFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setTheme(THEME); // Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.add_dialogue);
editText = (EditText) findViewById(R.id.input);
this.goButton = (Button) this.findViewById(R.id.go);
this.goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
TestFragment testFragment = new TestFragment();
Toast.makeText(getBaseContext(), "ok is clicked",
Toast.LENGTH_SHORT).show();
Log.d("dialogue", "ok is clicked");
testFragment.updateListView(strurl);
finish();
}
});
}
}
TestFragment.java
public final class TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
}
...
public void updateListView(String strurl) {
Log.d(TAG,"Url in updateListView : " + strurl);
if (isOnline()) {
animationLoadingImage.start();
loadingImage.setVisibility(View.VISIBLE);
new RequestTask().execute(strurl);
// openMenu(view);
} else {
// openMenu(view);
Toast.makeText(getActivity(),
"There is no network connection" + TAG, Toast.LENGTH_SHORT)
.show();
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getActivity()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
}
My Problem is
:
当我从活动按下按钮时,我收到了以下日志。
FATAL EXCEPTION: main
E/AndroidRuntime( 2316): java.lang.NullPointerException
E/AndroidRuntime( 2316): at com.mistral.droidcast.TestFragment.isOnline(TestFragment.java:363)
E/AndroidRuntime( 2316): at com.mistral.droidcast.TestFragment.updateListView(TestFragment.java:348)
E/AndroidRuntime( 2316): at com.mistral.droidcast.AddDialogue$1.onClick(AddDialogue.java:45)
E/AndroidRuntime( 2316): at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 2316): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 2316): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 2316): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2316): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2316): at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime( 2316): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2316): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
E/AndroidRuntime( 2316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
E/AndroidRuntime( 2316): at dalvik.system.NativeStart.main(Native Method)
E/ ( 122): Dumpstate > /data/log/dumpstate_app_error
W/ActivityManager( 122): Force finishing activity com.mistral.droidcast/.AddDialogue
我知道,我无法访问TestFragment.java中的任何视图,并且在我的代码中遗漏了一些内容。但是,我不知道那是什么。
请帮我解决。
提前感谢你!!!
答案 0 :(得分:2)
请检查TestFragment.isOnline(TestFragment.java:363)
我的猜测是getActivity()返回null。这意味着您的片段没有附加到您的活动中。您需要在此处How do I add a Fragment to an Activity with a programmatically created content view
中添加片段到活动中