Android Button Controller类

时间:2016-03-07 19:25:12

标签: android view imagebutton

我正在尝试创建一个类来处理我正在制作的应用程序中的大多数常用按钮。 我只是不知道该怎么做。我用一个ButtonPressed方法创建了一个类,它将执行按下的每个按钮的操作。 我尝试了几种方法,但我认为归结为我没有将观点传递给班级。有没有更好的方法呢? 感谢您的任何帮助。它给我的错误是Nullpoint引用。我认为这是由于不知道在哪里寻找视图。

这里的主要重点是允许此按钮控制器类通过调用按下的按钮来处理每个活动共有的按钮。每次我更改每项活动的内容时,我都不想继续复制和粘贴按钮控件。

这是buttonPressed类

public class ButtonControler extends AppCompatActivity {


public ButtonControler(){}

public void buttonPressed()
{
    //add the buttons contorls
    //if the top button is pushed get the promotion page
    final ImageButton promo = (ImageButton)findViewById(R.id.apivita);
    promo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent getPromos = new Intent(v.getContext(),PromotionMain.class);
            startActivity(getPromos);
        }
    });
    //if the button 1  is pushed get the defulat webview page
    final ImageButton button1 = (ImageButton)findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Button1: ", "Pressed");
            Intent button1Pressed = new Intent(v.getContext(),MyWebViewMain.class);
            startActivity(button1Pressed);
        }
    });


    //if the cart is pushed get the cart webview page
    final ImageButton cart = (ImageButton)findViewById(R.id.button2);
    cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Cart: ","Pressed");
            Intent myCart = new Intent(v.getContext(),Cart.class);
            startActivity(myCart);
        }
    });

    //if the button 3  is pushed get the defulat webview page
    final ImageButton button3 = (ImageButton)findViewById(R.id.button3);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Button3: ","Pressed");
            Intent buttonPressed = new Intent(v.getContext(),MyWebViewMain.class);
            startActivity(buttonPressed);
        }
    });

    //if the button 4  is pushed get the defulat webview page
    final ImageButton button4 = (ImageButton)findViewById(R.id.button4);
    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Button4: ","Pressed");
            Intent buttonPressed = new Intent(v.getContext(),MyWebViewMain.class);
            startActivity(buttonPressed);
        }
    });

    //if the Store list  is pushed get the defulat webview page
    final ImageButton storeList = (ImageButton)findViewById(R.id.button5);
    storeList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("StoreList: ","Pressed");
            Intent myStoreList = new Intent(v.getContext(),StoreList.class);
            startActivity(myStoreList);
        }
    });

    //if the Store list  is pushed get the notification
    final Button notification = (Button)findViewById(R.id.gcmlogo);
    notification.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Notificatiion: ","Pressed");
            Intent myNotification = new Intent(v.getContext(),GCMMainActivity.class);
            startActivity(myNotification);
        }
    });

    //if the Store list  is pushed get the notification
    final Button myPage = (Button)findViewById(R.id.mypage);
    myPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MyPage: ","Pressed");
            //read the stored values
            String storedUser = CustomerPerferences.readString(getApplicationContext(), CustomerPerferences.USER_NAME, "");
            String storedPass =CustomerPerferences.readString(getApplicationContext(), CustomerPerferences.PASSWORD, "");
            //see if use has entered a username and password
            if(storedUser.equals("USER_NAME"))
            {
                Log.d("Loging in", "Go!");
                Intent goToLogin = new Intent(v.getContext(),LoginPage.class);
                startActivity(goToLogin);
            }else
            {

                Intent goToLogin = new Intent(v.getContext(),CustomerPage.class);
                goToLogin.putExtra("user_name", storedUser);
                startActivity(goToLogin);
            }

        }
    });

这是将使用按钮控制器类

的主要页面之一的示例
public class PromotionMain extends AppCompatActivity {

//gobal variables
  //these are where the paths will be stored
final ArrayList<String> pictureArray = new ArrayList<String>();
final ArrayList<String> pathArray = new ArrayList<String>();
final ArrayList<String> labelArray = new ArrayList<String>();
//Promotiion website
final String promos = myUrl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.promotion_layout);

    //parse the JSON string
    //parse the JSON string
    JSONParser jp = new JSONParser();

    try {
        jp.parsesData(promos, pictureArray, pathArray, labelArray);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ArrayList<ListItem> listData = getListData();

    final ListView listView = (ListView) findViewById(R.id.custom_list);
    listView.setAdapter(new CustomListAdapter(this, listData));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        //here is a method to either send the user to the poduct page
        //or to the main page in the app
        //open a new activity and close this one down
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            ListItem promoData = (ListItem) listView.getItemAtPosition(position);
            //Toast.makeText(PromotionMain.this, "Selected :" + " " + promoData.getPathUrl(), Toast.LENGTH_LONG).show();
            //open up a new activity and send it to this site http://52.68.68.86/magento
            //lest open up the corrisponding webpage
            Intent reDirect = new Intent();
            reDirect.setAction(Intent.ACTION_VIEW);
            reDirect.addCategory(Intent.CATEGORY_BROWSABLE);
            //need to make sure that if it is not a url them do nothing in future rendtions
            //other wise it will crash the app
            reDirect.setData(Uri.parse(promoData.getPathUrl()));
            String newUrl = Uri.parse(promoData.getPathUrl()).toString();
            //check to see if there is a path for the promotion
            if (newUrl.contains("http")) {
                startActivity(reDirect);
            } else {
                //if not do nothing
                Log.d("Path URL: ", " is null");
            }

        }
    });

    //add the buttons contorls
   // buttonPressed();
    ButtonControler bc = new ButtonControler();
    bc.buttonPressed();
    //ButtonControler bc = new ButtonControler();
    //bc.pressed(promo,cart,storeList,button1,button3,button4,null);

}
private ArrayList<ListItem> getListData() {
    ArrayList<ListItem> listGetData = new ArrayList<ListItem>();


    //get the url for the image from the picture array list
    for (int i = 0; i < pictureArray.size(); i++) {
        ListItem imageData = new ListItem();
        imageData.setUrl(pictureArray.get(i));
        imageData.setPathUrl(pathArray.get(i));
        imageData.setLableTitle(labelArray.get(i));
        listGetData.add(imageData);

    }
    return listGetData;
}

4 个答案:

答案 0 :(得分:3)

首先改变这一点:

select a.id, b.id, c.id
from tbla a cross join tblb b cross join tblc c
where not exists (
  select 1
  from mytable t
  where t.fk_a = a.id
    and t.fk_b = b.id
    and t.fk_c = c.id
  )

public class ButtonControler extends AppCompatActivity

第二次将活动传递给public class ButtonControler

public void buttonPressed()

然后将每个findViewById更改为

public void buttonPressed(Activity activity)

从这样的活动中调用此方法

final ImageButton promo = (ImageButton)activity.findViewById(R.id.apivita);

您每次都在创建一个不同的活动,并在其上调用findViewById。哪个没有视图也没有开始。

答案 1 :(得分:1)

如果你想在按下按钮时去某个活动,你可以做一些简单的事情,你可以添加任何动作甚至调用函数

public class ListGroupsActivity extends AppCompatActivity {

    Button profile = (Button) findViewById(R.id.GoToProfile);
    Button wall =(Button) findViewById(R.id.GoToWall);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        profile.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                        Intent intent = new Intent(v.getContext(), ProfileActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                }
        );
        wall.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                        Intent intent = new Intent(v.getContext(),WallActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                }
        );
    }
}

答案 2 :(得分:1)

将ButtonControler更改为以下

final ImageButton promo = (ImageButton) mActivity.findViewById(R.id.apivita);

目前不知道你的包名,所以用你的.package.name.R

替换R.
final ImageButton promo = (ImageButton) mActivity.findViewById(com.example.pagerview.R.id.apivita);

[' O/F=7.1800E+00  PERCENT FUEL=  12.2249  EQUIVALENCE RATIO=5.5559E-01    DENSITY=9.5028E-01']
[]
['                 CHAMBER   THROAT     EXIT     EXIT']
[' PC/P             1.0000   1.7346   1.0083   62.915']
[' P', ' PSIA          1500.0    864.8   1487.7    23.84']
[' T', ' DEG R           5886     5555     5880     3494']
[' H', ' BTU/LB        -446.6   -674.1   -450.1  -1819.9']
[' S', ' BTU/(LB)(R)   2.3395   2.3395   2.3395   2.3395']
[' DEN (LBM/FT3)  6.50E-01 4.01E-01 6.45E-01 1.81E-02']
[' ']
[' M', ' MOL WT        27.357   27.616   27.361   28.499']
[' (DLV/DLP)T     -1.01645 -1.01277 -1.01639 -1.00034']
[' (DLV/DLT)P       1.3300   1.2733   1.3292   1.0126']
[' CP', 'BTU/(LB)(R)   0.9274   0.8626   0.9265   0.4371']
[' CP GAS(SF)       0.4353   0.4326   0.4353   0.4041']
[' GAMMA GAS(SF)    1.2001   1.1993   1.2001   1.2084']
[' GAMMA (S)        1.1390   1.1395   1.1390   1.1949']
[' SON VEL', 'FT/SEC   3490.4   3375.7   3488.6   2698.9']
[' MU', 'LBF-S/FT2   2.01E-06 1.93E-06 2.01E-06 1.41E-06']
[' K', 'LBF/S-DEGR   3.16E-02 3.01E-02 3.16E-02 2.03E-02']
[' PRANDTL NO      0.69205  0.69371  0.69207  0.70189']
[' MACH NUMBER      0.0000   1.0000   0.1202   3.0726']
[' ']
[' AE/AT                     1.0000   4.9993   9.0000']
[' CSTAR', ' FT/SEC               5139     5139     5139']
[' CF  VAC                    1.233             1.757']
[' CF                         0.657             1.614']
[' IVAC', 'LBF-S/LBM            197.00            280.59']
[' I', ' LBF-SEC/LBM            104.92            257.74']
[' MOL WT(MIX)      27.357   27.616   27.361   28.499']

答案 3 :(得分:0)

简单地说,让PromotionMain课程extends ButtonControler上课。而不是AppCompatActivity。