在安卓中找不到方法(视图)



>我在这里创建应用程序是此应用程序的链接在线联系当我通过连接移动设备从 android 工作室安装时,此应用程序运行良好。但是当我从 Play 商店安装它时,它会崩溃,我调试 Play 商店应用程序并发现一些意外的错误,因为当我直接从 android 工作室运行应用程序时没有这样的错误。

日志

 java.lang.IllegalStateException: Could not find method createAccount(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCreateAccount'
 D/AccessibilityManager: setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false, mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
                                                     java.lang.Throwable: setStateLocked

第二个不是错误,但它显示应用程序崩溃的时间。这些错误来自 登录活动 这里是代码。
登录活动

 public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
}
protected void login(View view){
    EditText etxtPhoneNumber = (EditText) findViewById(R.id.etxtPhoneNumber);
    EditText etxtPassword = (EditText) findViewById(R.id.etxtPassword);
    String phoneNumber = etxtPhoneNumber.getText().toString();
    String password = etxtPassword.getText().toString();
    String action = "login";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(action, phoneNumber, password);
}

protected void forgetPassActivity(View view){
    startActivity(new Intent(this, ForgetPassword.class));
}
protected void createAccount(View view){
    Intent loginIntent = new Intent(this, CreateAccount.class);
    startActivity(loginIntent);
}
@Override
public void onBackPressed() {
    moveTaskToBack(true);
}
}

主活动

 public class MainActivity extends AppCompatActivity {
public static String SHARED_PREF_FILENAME = "com.azeem.onlinecontact.MYSHAREDPREF";
SharedPreferences sharedPref;
InterstitialAd interstitialAd;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Placing Ads in Application
    MobileAds.initialize(this, "ca-app-pub-XXXX");
    AdView adView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-XXXXX");
    interstitialAd.loadAd(adRequest);
    interstitialAd.setAdListener(new AdListener(){
        @Override
        public void onAdLoaded() {
            if(interstitialAd.isLoaded()){
                interstitialAd.show();
            }
        }
    });
       sharedPref = getSharedPreferences(SHARED_PREF_FILENAME, MODE_PRIVATE);
        Boolean login = sharedPref.getBoolean("login", false);
        if (login) {
               // Send user to login Activity
            Intent mainActivity = new Intent(this, LoginActivity.class);
            startActivity(mainActivity);
       }
       // Rest of the code.

activity_login.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:layout_below="@+id/etxtPhoneNumber"
    android:layout_alignLeft="@+id/etxtPhoneNumber"
    android:layout_alignStart="@+id/etxtPhoneNumber"
    android:layout_marginTop="39dp"
    android:id="@+id/etxtPassword"
    android:hint="@string/enter_password" />
<Button
    android:text="@string/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/etxtPassword"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp"
    android:id="@+id/btnLogin"
    android:onClick="login"
    style="@style/Widget.AppCompat.Button.Colored" />
<Button
    android:text="@string/create_account"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="45dp"
    android:id="@+id/btnCreateAccount"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="createAccount"
    style="@style/Widget.AppCompat.Button.Colored" />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:layout_marginTop="147dp"
    android:id="@+id/etxtPhoneNumber"
    android:hint="@string/your_phone_number"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<TextView
    android:text="@string/online_contact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:layout_marginRight="38dp"
    android:layout_marginEnd="38dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="54dp" />
<TextView
    android:text="@string/foget_password_click_here"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnCreateAccount"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="24dp"
    android:id="@+id/tvForgetPassword"
    android:onClick="forgetPassActivity" />
    </RelativeLayout>    

我检查了一些堆栈溢出答案,当您没有声明onClick函数或未传递view等时,会出现这种错误。我认为还有其他一些错误。记得当我从 Android 工作室运行应用程序时,相同的代码工作正常

替换登录活动中的以下 3 个方法

public void login(View view){
    EditText etxtPhoneNumber = (EditText) findViewById(R.id.etxtPhoneNumber);
    EditText etxtPassword = (EditText) findViewById(R.id.etxtPassword);
    String phoneNumber = etxtPhoneNumber.getText().toString();
    String password = etxtPassword.getText().toString();
    String action = "login";
    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(action, phoneNumber, password);
}
public void forgetPassActivity(View view){
    startActivity(new Intent(this, ForgetPassword.class));
}
public void createAccount(View view){
    Intent loginIntent = new Intent(this, CreateAccount.class);
    startActivity(loginIntent);
}

最新更新