1

我创建了一个身份验证界面(登录名和密码)。这是代码:

package com.example.exercice1part2;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private String login="aaa";
private String pwd="bbb";
private EditText editText1;
private EditText editText2;
private Button bouton;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
    TextView text=new TextView(null);
    text.setText("entrer votre login et mot de passe");
    final EditText editText1= new EditText(null);
    final EditText editText2=new EditText(null);
  final Button bouton= new Button("con");
    TableLayout x=new TableLayout(this);
    x.addView(text);
    x.addView(editText1);
    x.addView(editText2);
    x.addView(bouton);
    bouton.setOnClickListener(new OnClickListener(){
         public void onClick (View v){
           if (((editText1.getText().toString().equals(login))&&(editText2.getText().toString().equals(pwd)))==true)
           {

          Toast.makeText(MainActivity.this,"connection valide",Toast.LENGTH_LONG).show();
           if (v==bouton){
              final String requete="http://www.google.fr/search?q="+editText1.getText();
          Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(requete));
          startActivity(intent);}

       }
           else
           {
               Toast.makeText(MainActivity.this,"Pas de Connection " ,Toast.LENGTH_LONG).show();    
               }
         }});


  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
}

但是当我尝试运行应用程序时出现此错误:

11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.os.Looper.loop(Looper.java:137)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at java.lang.reflect.Method.invoke(Method.java:511)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at dalvik.system.NativeStart.main(Native Method)
11-17 18:11:25.426: E/AndroidRuntime(1028): Caused by: java.lang.NullPointerException
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.view.View.<init>(View.java:3236)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.view.View.<init>(View.java:3295)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.widget.TextView.<init>(TextView.java:447)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.widget.TextView.<init>(TextView.java:442)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.widget.TextView.<init>(TextView.java:438)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at com.example.exercice1part2.MainActivity.onCreate(MainActivity.java:26)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.Activity.performCreate(Activity.java:5008)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-17 18:11:25.426: E/AndroidRuntime(1028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-17 18:11:25.426: E/AndroidRuntime(1028):     ... 11 more
11-17 18:15:40.177: I/Process(1028): Sending signal. PID: 1028 SIG: 9
4

3 回答 3

0

我不认为这TextView text=new TextView(null);是正确的。

尝试使用TextView text=new TextView(this);

于 2012-11-17T19:51:51.407 回答
0

这是我最终找到的解决方案,它有效:

public package com.example.exercice1part2;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private String login="aaa";
private String pwd="bbb";
private EditText editText1;
private EditText editText2;
private TextView text;
private Button bouton;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TableLayout x=new TableLayout(this);
    setContentView(x);
    text =new TextView(getApplicationContext());
    text.setText("entrer votre login et mot de passe");
    editText1= new EditText(this);
    if (editText1.getText().toString().length()==0)
        editText1.setError("nom ne dépasse pas 10 caractère");
    editText2=new EditText(this);
    editText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
    if (editText2.getText().toString().length()==0)
        editText2.setError("mot de passe ne dépasse pas 6 caractère");
  bouton= new Button(this);
  bouton.setText("connection" );

    x.addView(text);
    x.addView(editText1);
    x.addView(editText2);
    x.addView(bouton);
    bouton.setOnClickListener(new OnClickListener(){
         public void onClick (View v){

           if (((editText1.getText().toString().equals(login))&&(editText2.getText().toString().equals(pwd)))==true)
           {

          Toast.makeText(MainActivity.this,"connection valide",Toast.LENGTH_LONG).show();
           if (v==bouton){
              final String requete="http://www.google.fr/search?q="+editText1.getText();
          Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(requete));
          startActivity(intent);}

       }
           else
           {
               Toast.makeText(MainActivity.this,"Pas de Connection " ,Toast.LENGTH_LONG).show();    
               }

         }});


  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
   }
}

并感谢所有帮助过我的人 :))

于 2012-11-19T12:50:25.857 回答
0

您的 onClickListener 是一个“匿名内部类”。它可以访问它声明的变量、它的“包含类”(您的活动)中的变量、包级类(在当前包中未声明为“公共”)和公共变量。

您在 onCreate 方法中声明并初始化 editText2 。onCreate 方法运行、完成和销毁所有本地声明的变量。onCreate 在 onClickListener 可以运行之前完成,因为 UI 直到 onCreate 完成后才构建。因此,对 editText2 的引用不再存在,您将获得 NPE。

要修复它,请将声明移至类级别,即在 onCreate 之前:

...
final EditText editText1
...
@Override
public void onCreate(Bundle savedInstanceState) {
...

并在 onCreate 中对其进行初始化:

@Override
public void onCreate(Bundle savedInstanceState) {
...
editText1= new EditText(this);
...

祝你好运。

于 2012-11-17T20:22:43.063 回答