无法在Dialog Fragment中创建活动

时间:2014-05-29 18:33:42

标签: java android android-intent android-fragments

我有一个带有2个图像按钮的活动。活动扩展为DialogFragment。每当我尝试创建一个Intent时:

 Intent in = new Intent(this, ChatActivity.class)

建议删除参数时出错。如何在ImageButtons上创建这些Intent?

VotingActivity.java

public class VotingDialog extends DialogFragment{

private ImageButton isupport;
private ImageButton iagainst;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        final View view = inflater.inflate(R.layout.activity_voting_dialog, container);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        final ImageButton isupport = (ImageButton) view.findViewById(R.id.isupport);
        final ImageButton iagainst = (ImageButton) view.findViewById(R.id.iagainst);

        isupport.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(getActivity(), ChatActivity.java);
                            startActivity(intent);

            }
        });
                return view;

    }

}

logcat的:

05-30 00:33:20.865: W/IInputConnectionWrapper(10881): getSelectedText on inactive InputConnection
05-30 00:33:20.935: W/IInputConnectionWrapper(10881): setComposingText on inactive InputConnection
05-30 00:33:22.355: D/dalvikvm(10881): GC_CONCURRENT freed 2357K, 25% free 9992K/13315K, paused 14ms+20ms, total 126ms
05-30 00:33:22.455: E/SpannableStringBuilder(10881): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-30 00:33:22.455: E/SpannableStringBuilder(10881): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-30 00:33:23.205: E/SpannableStringBuilder(10881): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-30 00:33:23.205: E/SpannableStringBuilder(10881): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-30 00:33:24.645: D/AndroidRuntime(10881): Shutting down VM
05-30 00:33:24.645: W/dalvikvm(10881): threadid=1: thread exiting with uncaught exception (group=0x40fd4438)
05-30 00:33:24.665: E/AndroidRuntime(10881): FATAL EXCEPTION: main
05-30 00:33:24.665: E/AndroidRuntime(10881): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.social.knowtide/com.social.knowtide.ChatActivity}: java.lang.NullPointerException
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.os.Looper.loop(Looper.java:137)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread.main(ActivityThread.java:4963)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at java.lang.reflect.Method.invokeNative(Native Method)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at java.lang.reflect.Method.invoke(Method.java:511)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at dalvik.system.NativeStart.main(Native Method)
05-30 00:33:24.665: E/AndroidRuntime(10881): Caused by: java.lang.NullPointerException
05-30 00:33:24.665: E/AndroidRuntime(10881):    at com.social.knowtide.ChatActivity.onCreate(ChatActivity.java:40)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.Activity.performCreate(Activity.java:5184)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-30 00:33:24.665: E/AndroidRuntime(10881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
05-30 00:33:24.665: E/AndroidRuntime(10881):    ... 11 more

ChatActivity.java

public class ChatActivity extends Activity implements ChatCallbackAdapter  {
    SocketIO socket;
    ChatCallbackAdapter callback;
    Chat chat;
    TextView tv;
    EditText ed;
      String nickname; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        Bundle in = getIntent().getExtras();
        //final String disctext = in.getString("distext");
        final String disid = in.getString("disid");
        //final String detailsdesc = in.getString("detaildesc");
        Button button = (Button) findViewById(R.id.button1);
         tv = (TextView) findViewById(R.id.reply);
         ed = (EditText)findViewById(R.id.editText1);

        //TextView discussiontext = (TextView)findViewById(R.id.discussiontext1);
        //RelativeLayout rel = (RelativeLayout)findViewById(R.id.rellayout);
        //discussiontext.setText(disctext);
        /*rel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChatActivity.this,DiscussionDetailActivity.class);
                Bundle b = new Bundle();
                b.putString("disid", disid);
                b.putString("distext", disctext);
                b.putString("detaildesc",detailsdesc );
                i.putExtras(b);
                startActivity(i);
            }
        });*/


            this.runOnUiThread(new Runnable() {

            @Override
            public void run() {

                startChat();

            }

            private void startChat() {
                // TODO Auto-generated method stub
                tv.append("Connecting...");

                chat = new Chat(ChatActivity.this);
                chat.start();
            } });
             button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        nickname = new LoginSharePreferences().getName(ChatActivity.this);
                        System.out.println(disid);
                        chat.sendMessage(ed.getText().toString(),disid,nickname);
                        ed.setText("");
                    }
                });

    }

    @Override
    public void callback(JSONArray data) throws JSONException {}
    @Override
     public void on(String event,  final JSONObject obj1)  {

        if (event.equals("user message")) {

                this.runOnUiThread(new Runnable() {
                    JSONObject obj = obj1;
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                            try {
                                tv.append(obj.getString("user") + ": " + obj.getString("message")+ "\n");
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                    }
                });

            }

            else if (event.equals("announcement")) {

                this.runOnUiThread(new Runnable() {
                    JSONObject obj = obj1;
                    @Override
                    public void run() {

                        try {
                            tv.append(obj.getString("user") + " " + obj.getString("action") + "\n");
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });

            }

            /*else if (event.equals("nicknames")) {
                JSONArray names = obj1.names();
                String str = "";
                for (int i=0; i < names.length(); i++) {
                    if (i != 0)
                        str += ", ";
                    str += names.getString(i);
                }
                OnlineUsers.setText(str);
            }*/
    }

    @Override
     public void onMessage(String message) {}

    @Override
        public void onMessage(JSONObject json) {}

    @Override
        public void onConnect() {

        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                tv.append("done!\n");
                }   });



           // String nickname = new LoginSharePreferences().getName(this);
            //System.out.println(nickname + "kuch nhi aya");
            if (!nickname.equals(null)) {

                chat.join(nickname);
        //       tv.append( nickname +"join the debate" + "\n");
            }
    }


    @Override
        public void onDisconnect() {
           Toast.makeText(ChatActivity.this,"Connection lost",Toast.LENGTH_LONG).show();

        }

    @Override
        public void onConnectFailure() {
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                tv.append("error!\n");
            }
        });    

        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.chat, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

不在活动环境中,因此请使用Intent in = new Intent(getActivity() , ChatActivity.class) ;