如何在一个活动中使用的Java类? [英] How to use java classes within one activity?

查看:122
本文介绍了如何在一个活动中使用的Java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用Java类中的一个活动,我的意思是有一堆Java类中的活动起$ P $垫出不同的组件。我有点新的到Android,这是我试过到目前为止:

MainActivity.java

 包com.example.alex.myapplication;



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



 公共类MainActivity扩展ActionBarActivity {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
     新的东西(这一点);


}





@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.menu_main,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();

    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }

    返回super.onOptionsItemSelected(项目);
}
  }
 

Something.java

 进口android.view.View;
进口android.widget.EditText;
进口android.widget.Button;
进口android.app.Activity;




    公共类的东西{

  私人活动的活动;
  私人按键加,减,乘,devide;
  私人的EditText鲷,editB,editC;
  私人双人doubleA,doubleB,doubleC;





      公众的东西(活性){

              活性= A;
              点击();

       }


       公共无效点击(){
           鲷=(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());


              添加=(按钮)activity.findViewById(R.id.add);
              减=(按钮)activity.findViewById(R.id.subtract);
              乘=(按钮)activity.findViewById(R.id.multiply);
              devide =(按钮)activity.findViewById(R.id.devide);

               add.setOnClickListener(新View.OnClickListener(){
                   @覆盖
                   公共无效的onClick(视图v){

                     doubleC = doubleA + doubleB;
                     字符串s =+ doubleC;
                      editC.setText(多个);


                   }
               });

               subtract.setOnClickListener(新View.OnClickListener(){
               @覆盖
               公共无效的onClick(视图v){

                   doubleC = doubleA-doubleB;
                   字符串s =+ doubleC;
                   editC.setText(多个);


               }
           });

               multiply.setOnClickListener(新View.OnClickListener(){
               @覆盖
               公共无效的onClick(视图v){

                   doubleC = doubleA * doubleB;
                   字符串s =+ doubleC;
                   editC.setText(多个);


               }
           });



           devide.setOnClickListener(新View.OnClickListener(){
               @覆盖
               公共无效的onClick(视图v){

                   doubleC = doubleA / doubleB;
                   字符串s =+ doubleC;
                   editC.setText(多个);


               }
           });



       }



       }
 

所以我不知道为什么我的听众不工作对我的按钮,所以我试图传递活动具有监听器添加到按钮,但实际上没有工作的类现在我的申请将不即使在模拟器中启动。所有我想做的事是有MainActivity处理桂,并有东西级处理的听众,但无论我做什么,我似乎无法让他们相互沟通,形成一个活动。

LogCat中

 产生的原因:显示java.lang.NullPointerException:尝试在空对象引用调用虚拟方法android.view.View android.view.Window.findViewById(INT)'
        在android.app.Activity.findViewById(Activity.java:2071)
        在com.example.alex.myapplication.Something.click(Something.java:32)
        在com.example.alex.myapplication.MainActivity.onCreate(MainActivity.java:19)
        在android.app.Activity.performCreate(Activity.java:5937)
        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        在android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2251)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5221)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 

解决方案

据我所知,这将帮助别人谁遇到同样的问题,我将从中受益,问题是,我需要的活动传递到我的东西(活性){} 构造并初始化一切成。像东西(活性){
            AT1 =(EditText上)a.findViewById(R.id.numberOne);             AT2 =(EditText上)a.findViewById(R.id.numberTwo);             AT3 =(EditText上)a.findViewById(R.id.result);             添加=(按钮)a.findViewById(R.id.add);             减=(按钮)a.findViewById(R.id.subtract);             乘=(按钮)a.findViewById(R.id.multiply);             devide =(按钮)a.findViewById(R.id.devide); }

和所有你需要做的主要活动是通过活动,当你做出一个实例/执行code(例如:东西S =新的东西(这一点); /新的东西(这一点);)你后做到这一点,你可以叫s.click();它应该工作。

How can you use a java class within one activity, by that I mean is having different components of that activity spread out in a bunch of java classes. I'm a little new to android and this is what I have tried so far:

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);


               }
           });



       }



       }

So I wasn't sure why my listeners weren't working on my buttons so I tried passing the activity to the class that has the listeners added to the buttons but that didn't work in fact now my application won't even start in the emulator. All I wanted to do was have "MainActivity" handle the "Gui" and have the "Something" class handle the listeners but no matter what I do I can't seem to make them communicate with one another to form one 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)

解决方案

As I know this will help someone else who encountered the same problem as me will benefit, the problem was that I needed to pass the activity into my Something(Activity a){} constructor and initialize everything into that. Like 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); }

And all you need to do in main activity is pass the Activity when you make an instance/execute the code(ex: Something s = new Something(this);/new Something(this);) After you done that you can just call s.click(); and it should work.

这篇关于如何在一个活动中使用的Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆