从主要活动以外的活动访问片段

时间:2014-11-19 23:15:18

标签: java android android-activity android-fragments

我有一个现有的应用程序,我正在尝试修改,并且可以真正使用一些帮助。

这是一个聊天应用程序。申请的原始流程如下: Launch->启动屏幕活动 - > MainActivity(扩展Actionbar Sherlock)

一旦进入主活动,默认片段就是ChatRoomFragment。从那里,您可以选择不同的选项卡并与应用程序进行交互。

我想改变的流程如下: 启动 - >启动屏幕活动 - >服务条款/标志 - > MainMenu-> MainActivity

我创建了mainmenu布局,包含4个按钮。加入,搜索,个人资料,设置

这是问题所在。我的加入按钮工作正常,onClick只是触发意图启动MainActivity和聊天室加载。在此屏幕中,您可以访问应用程序中的不同选项卡和片段。

但是,我现在想要"搜索"按钮设置为打开一个对话框。使用editText字段和搜索按钮。单击搜索后,它应将搜索字符串传递给PlacesSearchFragment并填充结果。

我从我的应用程序中复制了代码,其中此搜索通常已完成(在ChatRoomsFragment内部,但它不能在我的mainMenu活动中工作。

如何从菜单活动启动新片段?

以下代码:

  menuActivity.java
package com.peekatucorp.peekatu;

//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class menuActivity extends Activity implements ActionBar.TabListener {
    Button b1;
    Button b2;
    Button b3;
    EditText txtsearch;
    final private static int DIALOG_LOGIN = 1;
    final private static int DIALOG_FORGET = 2;
    final private static int DIALOG_SEARCH = 3;
    private android.app.FragmentTransaction ft;



@Override
public void onCreate(Bundle savedInstanceState) {

SharedPreferences preferences = this.getSharedPreferences("MyPreferences", MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();


super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);

if(preferences.getString("Username", "").length()<=0 || preferences.getString("loggedin_user", "").length()<=0){
    showDialog(DIALOG_LOGIN);

}

b1= (Button) this.findViewById(R.id.joinbutton);

b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(menuActivity.this, MainActivity.class);
        menuActivity.this.startActivity(intent);

        SharedPreferences preferences = getSharedPreferences("MyPreferences", MODE_PRIVATE);

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("loadMain", "1");
        editor.commit();
        }

});
b2= (Button) this.findViewById(R.id.searchbutton);

b2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showDialog(DIALOG_SEARCH);
        }

    }

);





}

@Override
protected Dialog onCreateDialog(int id) {

    AlertDialog dialogDetails = null;

    switch (id) {


    case DIALOG_LOGIN:
                if(true){
....some code}


         break;
            case DIALOG_FORGET:
                if(true){
...some code
}
                break;
            case DIALOG_SEARCH:
            if(true){
                LayoutInflater inflater = LayoutInflater.from(this);
                View dialogview = inflater.inflate(R.layout.menusearch_layout, null);

                AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);

                dialogbuilder.setTitle("Where ya headed?");
                dialogbuilder.setView(dialogview);
                dialogDetails = dialogbuilder.create();
            }
    }

    return dialogDetails;
}
 @Override
    protected void onPrepareDialog(int id, Dialog dialog) {

        switch (id) {
        case DIALOG_LOGIN:
...some code
break;

case DIALOG_SEARCH:
                final AlertDialog alertDialog3 = (AlertDialog) dialog;
                final Button btnLocalsearch = (Button) alertDialog3
                        .findViewById(R.id.local_search);
                final Button btnSearch = (Button) alertDialog3
                        .findViewById(R.id.btn_search);
                final EditText txtsearch = (EditText) alertDialog3
                        .findViewById(R.id.txtsearch);

                btnSearch.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(final View v) {
                        //showDialog(DIALOG_FORGET);
                        //alertDialog3.dismiss();
                                // TODO Auto-generated method stub
                               menuActivity m = com.peekatucorp.peekatu.menuActivity.this;
                                final TabInfo tab = com.peekatucorp.peekatu.menuActivity.this.getCurrentTabInfo();
                                final PlacesSearchFragment fragment = new PlacesSearchFragment().setNAV(m).setSearch(txtsearch.getText().toString(),"1");
                                // fragment.setText(characters[position]);

                                // second, you push the fragment. It becomes visible and the up button is
                                // shown
                                m.pushFragment(tab, fragment);


                            }



        });
    }

mainmenu.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:layout_marginLeft="10dip"
        android:src="@drawable/registration_banner3"
        android:id="@+id/imageView" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Join Chat"
        android:id="@+id/joinbutton"
        android:layout_below="@+id/imageView"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:id="@+id/searchbutton"
        android:layout_below="@+id/joinbutton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="55dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Profile"
        android:id="@+id/prfbutton"
        android:layout_below="@+id/searchbutton"
        android:layout_marginTop="72dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignLeft="@+id/searchbutton"
        android:layout_alignStart="@+id/searchbutton" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:id="@+id/settingsbutton"
        android:layout_below="@+id/prfbutton"
        android:layout_marginTop="51dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <LinearLayout android:id="@+id/footer"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@layout/footer_repeat"
        android:layout_alignParentBottom="true">



    </LinearLayout>
</RelativeLayout>

ChatRoomsFragment.java(WORKING FRAGMENT)

public class ChatRoomsFragment extends SherlockFragment implements OnItemSelectedListener{
    String[] items;
    List<String> list;
    Spinner my_spin;
     RadioButton mainRoom;
     RadioButton customRoom;
     RadioButton GPSRoom;
     EditText privateRoom;
     EditText GPSsearch;
     TextView GPSaddress;
     String selected_public;
     Context contexxt;
     ImageLoader imageLoader;
     public AbstractTabStackNavigationActivity navact;
    @Override

      public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
          setRetainInstance(true);

          final View v = inflater.inflate(R.layout.chatrooms_layout, container, false);
          contexxt = v.getContext();
         // setRetainInstance(true);
          SharedPreferences preferences = v.getContext().getSharedPreferences("MyPreferences", this.getActivity().MODE_PRIVATE);
          my_spin=(Spinner)v.findViewById(R.id.spinner1);
          my_spin.setOnItemSelectedListener(this);
          selected_public = preferences.getString("selected_room", "Adult Lobby");
          AsyncHttpClient  client = new AsyncHttpClient();
            RequestParams params = new RequestParams();

            GPSsearch = (EditText)v.findViewById(R.id.cr_gps_search);
            GPSaddress = (TextView)v.findViewById(R.id.cr_gps_address);
            GPSaddress.setText(preferences.getString("user_location", ""));
            Button search_go = (Button)v.findViewById(R.id.cr_go_search);
            Button address_go = (Button)v.findViewById(R.id.cr_go_address);
            Button changeroom = (Button)v.findViewById(R.id.cr_changeroom);
            //Button changeroom2 = (Button)v.findViewById(R.id.cr_changeRoom2);


    search_go.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 MainActivity m = (MainActivity)getActivity();
                 final TabInfo tab = m.getCurrentTabInfo();
                    final PlacesSearchFragment fragment = new PlacesSearchFragment().setNAV(m).setSearch(GPSsearch.getText().toString(),"1");
                   // fragment.setText(characters[position]);

                    // second, you push the fragment. It becomes visible and the up button is
                    // shown
                    m.pushFragment(tab, fragment);  
            }
        });

有人可以向我解释如何让它加载片段。谢谢。如果我遗漏任何相关代码,请告诉我。我得到一个空指针异常作为我的错误。

1 个答案:

答案 0 :(得分:0)

嗯,首先,这是我在评论中谈到的代码:

 btnSearch.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(final View v) {
         menuActivity m = dialog.getOwningActivity();
         final TabInfo tab = m.getCurrentTabInfo();
         final PlacesSearchFragment fragment = new PlacesSearchFragment().setNAV(m).setSearch(txtsearch.getText().toString(),"1");
         m.pushFragment(tab, fragment);
         ...

然而,现在我输入了这个,就像你说的那样NPE正在调用pushFragment是没有意义的。如果活动外部类引用确实是空指针,那么它应该早先崩溃几行,调用getCurrentTabInfo。因此,我认为此代码更改不会有所帮助。请再看看你看到的堆栈,并告诉我NPE正在发生什么样的行。