0

我尝试在一个名为 UserProfile() 的类中更改 3 个文本视图,从类 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 文件,如您所见,前 2 个标志打印正确,错误是当我尝试在 TextView 上设置另一个文本时,但字符串的值(nname、nsurname 和 nemail)不为空原因也打印在控制台 HELP 中!

    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

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

谢谢。

4

1 回答 1

0

Android 中的活动是独立的容器,决不允许直接相互接触。这是因为 Android 背后的应用程序堆栈会在活动不再处于前台时“冻结”它们,甚至可以根据操作系统的判断将其交换到硬盘存储以节省 RAM。

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

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

于 2013-05-14T22:38:04.323 回答