尝试从另一个类覆盖TextView中的文本时Android应用程序崩溃

时间:2013-05-14 22:29:39

标签: java android nullpointerexception android-activity invocationtargetexception

我尝试在名为UserProfile()的类中更改3个textviews,从类UpdateProfile()调用方法update(),类UserProfile执行如下操作:

package com.safm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class UserProfile extends Activity {
TextView username, usersurname, useremail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    username = (TextView) findViewById(R.id.username);
    usersurname = (TextView) findViewById(R.id.usersurname);
    useremail = (TextView) findViewById(R.id.useremail);
}

public void updateButton(View view){
    Intent i = new Intent(this, UpdateProfile.class);
    startActivity(i);
}

public void update(String nname, String nusername, String nemail){
    System.out.println("2");
    System.out.println(nname);
    username.setText(nname);
    usersurname.setText(nusername);
    useremail.setText(nemail);
    System.out.println("3");
}

}

updateButton方法调用UpdateProfile类:

package com.safm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateProfile extends Activity {
EditText newusernametxt, newsurnametxt, newemailtxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_actualizarperfil);

    newusernametxt = (EditText) findViewById(R.id.newusernametxt);
    newsurnametxt = (EditText) findViewById(R.id.newsurnametxt );
    newemailtxt = (EditText) findViewById(R.id.newemailtxt );
}

public void updateInfo(View view){      
    String nname = newusernametxt .getText().toString();
    String nsurname = newsurnametxt .getText().toString();
    String nemail = newemailtxt .getText().toString();

    if(nname.compareTo("") != 0 && nsurname.compareTo("") != 0 && nemail.compareTo("") != 0){           
        UserProfile profile = new UserProfile();
        System.out.println("1");

        profile.update(nname, nsurname, nemail);
        System.out.println("4");

        Toast.makeText(this, "Success, the profile has been updated", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "You can't empty fields", Toast.LENGTH_SHORT).show();
    }

}

这是我的logcat文件,正如您所看到的,前两个标志打印正确,错误是当我尝试在TextView上设置另一个文本时,但字符串的值(nname,nsurname和nemail)是非空原因也打印在控制台帮助!

    05-14 21:43:31.140: I/System.out(878): 1
    05-14 21:43:31.140: I/System.out(878): 2
    05-14 21:43:31.150: I/System.out(878): TextEditValue

    05-14 21:43:31.150: D/AndroidRuntime(878): Shutting down VM
    05-14 21:43:31.150: W/dalvikvm(878): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    05-14 21:43:31.310: E/AndroidRuntime(878): FATAL EXCEPTION: main
    05-14 21:43:31.310: E/AndroidRuntime(878): java.lang.IllegalStateException: Could not execute method of the activity
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.view.View$1.onClick(View.java:3599)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.view.View.performClick(View.java:4204)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.view.View$PerformClick.run(View.java:17355)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.os.Handler.handleCallback(Handler.java:725)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.os.Handler.dispatchMessage(Handler.java:92)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.os.Looper.loop(Looper.java:137)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at java.lang.reflect.Method.invokeNative(Native Method)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at java.lang.reflect.Method.invoke(Method.java:511)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at dalvik.system.NativeStart.main(Native Method)
    05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.reflect.InvocationTargetException
    05-14 21:43:31.310: E/AndroidRuntime(878):  at java.lang.reflect.Method.invokeNative(Native Method)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at java.lang.reflect.Method.invoke(Method.java:511)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at android.view.View$1.onClick(View.java:3594)
    05-14 21:43:31.310: E/AndroidRuntime(878):  ... 11 more
    05-14 21:43:31.310: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
    05-14 21:43:31.310: E/AndroidRuntime(878):  at com.safm.UserProfile.update(UserProfile.java:31)
    05-14 21:43:31.310: E/AndroidRuntime(878):  at com.safm.UserProfile.updateInfo(UpdateProfile.java:33)
    05-14 21:43:31.310: E/AndroidRuntime(878):  ... 14 more

注意:我已经清理了项目,并在onCreate方法之外的ProfileUser类和UpdateProfile中的TextEdit中定义了TextView,但是不起作用。

感谢。

1 个答案:

答案 0 :(得分:0)

Android中的活动是单独的容器,绝不允许彼此直接接触。这是因为Android背后的应用程序堆栈“冻结”活动,当它们不再处于前台时,甚至可以交换到硬盘存储器,以便在操作系统自行决定的情况下保存在RAM上。

您的UserProfile封装应该在UpdateProfile活动关闭后重新加载其数据,或通过startActivityForResult以正确的方式请求回复。

从每个角度来看,您当前的实施在技术上都是不正确的,因为您要将UpdateProfile活动硬编码为始终从UserProfile活动调用。如果有一天你引入一个新的菜单选项直接从主屏幕调用它会怎么样?活动总是分开的,不应该做出这样的假设。