从另一个类调用我的主要活动方法之一返回错误

时间:2016-10-07 15:33:11

标签: java android nullpointerexception

主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
    startActivity(intent);
}


public void function(String data) {
    EditText editText=(EditText)findViewById(R.id.EditText);
    editText.setText(data);

}

}

这是我的第二次活动

public class SecondActivity extends Activity {
MainActivity mainActivity=new MainActivity();
@Override
public void onCreate(Bundle save){
    super.onCreate(save);
    setContentView(R.layout.layout2);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String data="SUCCESS!";
            mainActivity.function(data);
        }
    });

}

}

这是错误

10-07 20:56:47.678 11920-11920/wifiexplorer.braveheart.com.classtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: wifiexplorer.braveheart.com.classtest, PID: 11920
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
   at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
   at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
   at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
   at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
   at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
   at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:170)
   at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:502)
   at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:174)
   at wifiexplorer.braveheart.com.classtest.MainActivity.function(MainActivity.java:20)
   at wifiexplorer.braveheart.com.classtest.SecondActivity$1.onClick(SecondActivity.java:22)
   at android.view.View.performClick(View.java:5277)
   at android.view.View$PerformClick.run(View.java:21711)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:207)
   at android.app.ActivityThread.main(ActivityThread.java:5896)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

我是Android编程的新手,在这里我只是使用一个意图来调用我的第二个Activity。

2 个答案:

答案 0 :(得分:1)

使用此文档"Getting a Result from an Activity"

您想要更新您的ui元素。在您的情况下,良好的做法是使用 startActivityForResult 。您的案例中的示例代码:

 public class MainActivity extends AppCompatActivity {

    public static final String STRING_RESULT_DATA_KEY = "result_data";

    private static final int REQUEST_CODE_SECOND_ACTIVITY = 101;

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

        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, REQUEST_CODE_SECOND_ACTIVITY);
    }

    public void function(String data) {
        EditText editText=(EditText)findViewById(R.id.EditText);
        editText.setText(data);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REQUEST_CODE_SECOND_ACTIVITY) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                String dataString = data.getStringExtra(STRING_RESULT_DATA_KEY);
                function(dataString);
            }
        }
    }
}

SecondActivity:

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "SUCCESS!";
                Intent intent = new Intent();
                intent.putExtra(MainActivity.STRING_RESULT_DATA_KEY, data);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

答案 1 :(得分:0)

主要问题是您正在更改新创建的活动的可视元素,而不一定是绘制或膨胀。为什么不导航到你点击的主活动页面并将变量设置为你想要显示的文本,而不是在一个尚不存在的页面上调用该函数?