2017-01-06 13 views
0

が、私はこのアプリに問題があり、それは私を見るパスワードの変更アクティビティ

MainActivity.class

 package ahmedchtn.stockmanager; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    EditText username; 
    EditText password; 
    Button loginbutton; 
    Button bpasschange; 

    int counter = 3; 
    String username_string ; 
    String password1 = ""; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     username = (EditText) findViewById(R.id.identifer); 
     password = (EditText) findViewById(R.id.password); 
     loginbutton = (Button) findViewById(R.id.bLogin); 
     bpasschange = (Button) findViewById(R.id.bCh); 
     username_string = "admin"; 
     if (password1 == "") { 
      password1 = "admin"; 
     } else { 
      Intent intent4 = getIntent(); 
      password1 = intent4.getStringExtra("oldpassw"); 
     } 
     bpasschange.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       Intent intentoldpass = new Intent(MainActivity.this, PasswordChange.class); 
       intentoldpass.putExtra("oldpassw", password1); 

       Intent intentpasschan = new Intent(getApplicationContext(), PasswordChange.class); 
       startActivity(intentpasschan); 

      } 
     }); 

     loginbutton.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View view) { 

               if (username.getText().toString().equals(username_string) && 
                 password.getText().toString().equals(password1)) { 
                Toast.makeText(getApplicationContext(), 
                  "Redirecting...", Toast.LENGTH_SHORT).show(); 
                Intent iop = new Intent(getApplicationContext(), ManagementPage.class); 
                startActivity(iop); 


               } else { 
                Toast.makeText(getApplicationContext(), 
                  "Wrong Entries", Toast.LENGTH_SHORT).show(); 
                counter--; 
                if (counter == 0) { 
                 loginbutton.setEnabled(false); 
                } 


               } 
              } 
             } 

     ); 


    } 
} 

PasswordChange.class

package ahmedchtn.stockmanager; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class PasswordChange extends AppCompatActivity { 
    Button bchange; 
    EditText oldpassword; 
    EditText newpassword; 
    String oldpass; 
    String newpass; 

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


     bchange = (Button) findViewById(R.id.bChange2); 
     oldpassword = (EditText) findViewById(R.id.old_password); 
     newpassword = (EditText) findViewById(R.id.new_password); 
     bchange.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent ioldpass = getIntent(); 
       oldpass = ioldpass.getStringExtra("oldpassw"); 
       if (oldpass == (oldpassword.getText().toString())) { 
        newpass = newpassword.getText().toString(); 
        Intent intentnew = new Intent(PasswordChange.this, MainActivity.class); 
        intentnew.putExtra("passnew", newpass); 
        startActivity(intentnew); 

        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(intent); 
       } 
      } 
     }); 
    } 

} 

私は思う "と残念ながら、アプリは停止しました"問題は意思であることを.. あなたの助けを前もってありがとう! は、他のソリューション

logcat

E/AndroidRuntime: FATAL EXCEPTION: main 
        java.lang.NullPointerException 
         at ahmedchtn.stockmanager.MainActivity$2.onClick(MainActivity.java:46) 
         at android.view.View.performClick(View.java:4439) 
         at android.widget.Button.performClick(Button.java:139) 
         at android.view.View$PerformClick.run(View.java:18395) 
         at android.os.Handler.handleCallback(Handler.java:725) 
         at android.os.Handler.dispatchMessage(Handler.java:92) 
         at android.os.Looper.loop(Looper.java:176) 
         at android.app.ActivityThread.main(ActivityThread.java:5317) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
         at dalvik.system.NativeStart.main(Native Method) 
Application terminated. 

のXmlコード

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="ahmedchtn.stockmanager.MainActivity"> 


    <EditText 
     android:id="@+id/identifer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/activity_vertical_margin" 
     android:hint="@string/enter_your_id" /> 

    <EditText 
     android:id="@+id/password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/activity_vertical_margin" 
     android:hint="@string/enter_your_password" 
     android:inputType="textPassword" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <Button 
      android:id="@+id/bLogin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/login" /> 

     <Button 
      android:id="@+id/bCh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="changer passe" /> 

    </LinearLayout> 


</LinearLayout> 

activity_password_change.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_password_change" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="ahmedchtn.stockmanager.PasswordChange"> 

    <EditText 
     android:id="@+id/old_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/activity_vertical_margin" 
     android:hint="@string/enter_your_old_password" /> 

    <EditText 
     android:id="@+id/new_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/activity_vertical_margin" 
     android:hint="@string/enter_your_new_password" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="@dimen/activity_vertical_margin" 
     android:weightSum="1"> 

     <Button 
      android:id="@+id/bChange2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/activity_vertical_margin" 
      android:layout_weight="0.5" 
      android:text="@string/confirm" /> 

     <Button 
      android:id="@+id/main_page" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/activity_vertical_margin" 
      android:layout_weight="0.5" 
      android:text="@string/main_page" /> 


    </LinearLayout> 
</LinearLayout> 

マニフェスト activity_main.xmlあり

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="ahmedchtn.stockmanager"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".ManagementPage" 
      android:parentActivityName=".MainActivity" /> 
     <activity android:name=".ProductsSearch" /> 
     <activity android:name=".ProductsCommand" /> 
     <activity 
      android:name=".Editor" 
      android:parentActivityName=".ProductsList" /> 
     <activity android:name=".ProductsList" /> 

     <provider 
      android:name=".data.ProductProvider" 
      android:authorities="ahmedchtn.stockmanager" 
      android:exported="false" /> 

     <activity android:name=".PasswordChange"></activity> 
    </application> 

</manifest> 

私はあなたのManifest.xmlファイルでPasswordChange.classを定義していることを確認してください私のコード

+0

コメントは拡張された説明ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/132584/discussion-on-question-by-tuniprocoder-change-password-activity)。 –

答えて

0

を更新しました。

例は<activity android:name=".PasswordChange"/>です。これは、新しいアクティビティに行くときにアプリがクラッシュする最も一般的な理由です。

あなたがこれまでに(PasswordChange.classのライン28上のNPE)、この行が問題を引き起こしているように見えるに提供した情報から、
+0

アプリは開かずに最初から粉砕されました – tuniprocoder

+0

@tuniprocoder、これが実行可能な解決策である理由です。 'manifest.xml'を投稿できますか? – Pztar

+0

マニフェストを追加しました – tuniprocoder

1

:あなたはの両方でbChangeという名前のボタンを持って

bchange = (Button) findViewById(R.id.bChange); 
bchange.setOnClickListener(new View.OnClickListener() { 
... 

あなたのレイアウトxmlファイル - それらの1つをユニークなものに変更します。誤って間違ったリソースを参照している可能性がなくなるため、すべてのコンポーネントに一意の名前を付けることを強くお勧めします。


例:

activity_password_change.xml

... 
<Button 
    android:id="@+id/bPasswordChange" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/activity_vertical_margin" 
    android:layout_weight="0.5" 
    android:text="@string/confirm" /> 
... 

PasswordChange。MainActivity.javaに発生しています第二の問題については、Java

... 
bchange = (Button) findViewById(R.id.bPasswordChange); 
... 

Boolean bmain; 

あなたが宣言したので:

Intent intentbool = getIntent(); 
bmain = intentbool.getBooleanExtra("bool", bmain); 

はしかし、あなたの問題は、ここでライン20でありますBooleanオブジェクトではなく、booleanプリミティブあなたが初期化していないので、デフォルトでnullです。プリミティブに変更してください:

boolean bmain; 
+0

xmlコードを追加しました – tuniprocoder

+0

多くのインテントよりも最適化されたソリューションがある場合 – tuniprocoder

+0

私の答えが更新されました。 'bChange'という名前の2つのボタンが各レイアウトに1つあります。そのうちの1つを別のものに変更してください。すべてのIDは一意でなければなりません。 –

関連する問題