如何在一个活动中使用java类?

时间:2015-03-31 23:43:06

标签: java android

如何在一个活动中使用java类,我的意思是将该活动的不同组件分布在一堆java类中。我对android有点新鲜,这是我到目前为止所尝试的:

MainActivity.java

package com.example.alex.myapplication;



 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;



 public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     new Something(this);


}





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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
  }

Something.java

import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;




    public class Something  {

  private Activity activity;
  private Button add,subtract,multiply,devide;
  private EditText editA, editB, editC;
  private double doubleA,doubleB,doubleC;





      public Something(Activity a){

              activity=a;
              click();

       }


       public void click(){
           editA = (EditText) activity.findViewById(R.id.editText);
           editB = (EditText) activity.findViewById(R.id.editText2);
           editC = (EditText) activity.findViewById(R.id.editText3);
          doubleA =Double.parseDouble(editA.getText().toString());
          doubleB =Double.parseDouble(editB.getText().toString());


              add = (Button) activity.findViewById(R.id.add);
              subtract = (Button) activity.findViewById(R.id.subtract);
              multiply = (Button) activity.findViewById(R.id.multiply);
              devide = (Button) activity.findViewById(R.id.devide);

               add.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {

                     doubleC = doubleA+doubleB;
                     String s = "" + doubleC;
                      editC.setText(s);


                   }
               });

               subtract.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   doubleC = doubleA-doubleB;
                   String s = "" + doubleC;
                   editC.setText(s);


               }
           });

               multiply.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   doubleC = doubleA*doubleB;
                   String s = "" + doubleC;
                   editC.setText(s);


               }
           });



           devide.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   doubleC = doubleA/doubleB;
                   String s = "" + doubleC;
                   editC.setText(s);


               }
           });



       }



       }

所以我不确定为什么我的听众没有按下我的按钮,所以我尝试将活动传递给那些将听众添加到按钮但是实际上没有工作的类现在我的应用程序甚至无法在模拟器中启动。我想做的就是拥有" MainActivity"处理" Gui"并拥有" Something" class处理听众,但无论我做什么,我似乎无法让他们彼此沟通以形成一个Activity。

logcat的

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.Activity.findViewById(Activity.java:2071)
        at com.example.alex.myapplication.Something.click(Something.java:32)
        at com.example.alex.myapplication.MainActivity.onCreate(MainActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run           (ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

4 个答案:

答案 0 :(得分:1)

就像一个带有1个java类的XD之前的帖子,按下按钮后再次调用它,例如,你可以有一个看不见的按钮,无论你想要什么看不见的单选按钮,只需点击它就会变得可见并对你有用,这里我去:

首先控制活动的变量将要进行

String num ="";

然后有你的按钮,我使用其中2个而其他的是不可见的

Button bn1;
Button bn2;
Button bn3;
Button bn4;
bn3.setVisibility(View.INVISIBLE);
bn4.setVisibility(View.INVISIBLE);

然后按钮代码取决于你想要的按钮数量

Button.setOnClickListener(new Button onclickListener(){
        public void onClick(){
            //get a default variable in this case String num
    Intent intent = new Intent(MainActivity.this, MainActivity.class); num="cero"; intent.putExtra("po", num);
                        CodigoPeticion=2; startActivityForResult (intent,CodigoPeticion); finish(); break;
                }
            }
        });

这个获得String num:

Bundle extras = getIntent().getExtras();
        if (extras!= null) {
            num =extras.getString("po");
        }

最后但并非最不重要的是这个根据字符串来做某事:

    if (num.matches("cero")){
                //do something, enable more buttons, disable radiobuttons,
bn3.setVisibility(View.VISIBLE);
            }else if(num.matches("one")){//this string is from another button
                //do something else in the same activity, as you spected enable radiobuttons, show a image, etc
bn4.setVisibility(View.VISIBLE);
            }else{
                //some textview with a specific title
                TextView.setText("Something's Wrong");
            }

不要忘记bn3和bn4听众!

希望能帮助你,看到你!

答案 1 :(得分:1)

我认为你应该重新安排代码以在something构造函数中包含onClickListener:

Something.java

import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;

public class Something {

    private Activity activity;
    private Button add, subtract, multiply, devide;
    private EditText editA, editB, editC;
    private double doubleA, doubleB, doubleC;

    public Something(Activity a) {

        activity = a;

        editA = (EditText) activity.findViewById(R.id.editText);
        editB = (EditText) activity.findViewById(R.id.editText2);
        editC = (EditText) activity.findViewById(R.id.editText3);

        add = (Button) activity.findViewById(R.id.add);
        subtract = (Button) activity.findViewById(R.id.subtract);
        multiply = (Button) activity.findViewById(R.id.multiply);
        devide = (Button) activity.findViewById(R.id.devide);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                click(1);

            }
        });

        subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                click(2);

            }
        });

        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                click(3);
            }
        });

        devide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                click(4);
            }
        });
    }


    public void click(int calculate) {

        // assume number entered - no error
        doubleA = Double.parseDouble(editA.getText().toString());
        doubleB = Double.parseDouble(editB.getText().toString());

        switch (calculate) {

            case 1:
                doubleC = doubleA + doubleB;
                String s = "" + doubleC;
                editC.setText(s);
                break;

            case 2:
                doubleC = doubleA - doubleB;
                String s = "" + doubleC;
                editC.setText(s);
                break;

            case 3:
                doubleC = doubleA * doubleB;
                String s = "" + doubleC;
                editC.setText(s);
                break;

            case 4:
                doubleC = doubleA / doubleB;
                String s = "" + doubleC;
                editC.setText(s);
                break;

            default:
                break;

        }

    }

}

我认为问题在于点击()'主构造函数内部的方法仅运行一次代码。我希望这有帮助!

答案 2 :(得分:1)

您可以在主要活动中使用“某事”类。

首先通过

初始化主要活动中的“Something”类
Something s = new Something(MainActivity.this);

你可以在主Activity.Just中使用该类的每个方法,就像你想要的那样,

s.add.setonclicklistener(...

s.click();
//Just use like this.

答案 3 :(得分:1)

据我所知,这将有助于遇到与我相同问题的其他人受益,问题是我需要将活动传递到我的Something(Activity a){}构造函数并将所有内容初始化为该内容。与Something(Activity a){
aT1 = (EditText) a.findViewById(R.id.numberOne); aT2 = (EditText) a.findViewById(R.id.numberTwo); aT3 = (EditText) a.findViewById(R.id.result); add = (Button) a.findViewById(R.id.add); subtract = (Button) a.findViewById(R.id.subtract); multiply = (Button) a.findViewById(R.id.multiply); devide = (Button) a.findViewById(R.id.devide); }

一样

你需要在main活动中做的就是在你创建一个实例/执行代码时传递Activity(例如:Something s = new Something(this); / new Something(this);)在你完成之后你可以只调用s.click();它应该工作。