我创建了一个身份验证界面(登录名和密码)。这是代码:
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