0

我正在使用ActionBarSherlockand编写应用程序viewPagerIndicator

My Aim is

要启动对话活动(AddDialogue.java),当我从MainScrenActivity.java. 通过按下按钮AddDialogue.java,它应该检查网络连接并显示项目listview

AddDialogue.java我正在使用 updateListview() 方法,该方法在 testFragment 类中定义。

添加对话.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();
            }
        });
    }
}

测试片段.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 访问任何视图,并且在我的代码中遗漏了一些东西。但是,我不知道那是什么。

请帮我解决它。

提前谢谢你!!!

4

1 回答 1

2

请检查线路发生了什么TestFragment.isOnline(TestFragment.java:363)

我的猜测是 getActivity() 返回 null。这意味着您的片段尚未附加到您的活动中。您需要将片段添加到活动中,如此处所述如何使用以编程方式创建的内容视图将片段添加到活动中

于 2013-05-25T07:11:04.027 回答