美好的每一天!
我是 android 开发的初学者,当我从第二个活动切换到第三个活动时遇到了一些麻烦:我在第三个活动的 onCreate 函数中遇到了一个由 NullPointerExeception 引起的强制子句。我阅读了堆栈跟踪,发现我的问题来自第二个活动的第 60 行。
name_server = thisIntent.getExtras().getString("ip");
我不明白为什么,所以我不知道如何解决它:/
以下是来源:
第一项活动:
package Smart.Wifi.Mote.v2;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SmartWifiMotev22 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**********************************
Clic sur "connexion"
**********************************/
OnClickListener clic = new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// déclaration de la nouvelle fenêtre
// @1 : fenêtre actuelle
// @2 : nouvelle fenêtre
Intent newpage = new Intent(SmartWifiMotev22.this, mainActivityv22.class);
System.out.println("lancement fenêtre");
// récupération de la valeur contenue dans la zone de texte (adresse ip)
EditText a_IP = (EditText) findViewById(R.id.T_IP);
String adresse_IP = a_IP.getText().toString();
System.out.println("adresse_IP : " + adresse_IP);
// ajout des valeurs à l'intent (fenêtre)
newpage.putExtra("ip", adresse_IP);
startActivity(newpage); // lancement de la fenêtre
}
} //
} //
第二个 :
package Smart.Wifi.Mote.v2;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
public class mainActivityv22 extends Activity
{
public String name_server = null;
public Socket s = null;
public String message = null;
public float startXPosition;
public float startYPosition;
public float endXPosition;
public float endYPosition;
public int moveX, moveY;
public Thread cThread;
public Thread sendThread;
public Thread alive;
public Intent thisIntent;
public boolean stopconnection = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
thisIntent = getIntent();
// récupération de l'adresse IP envoyé en paramètre via intent.putExtra()
name_server = thisIntent.getExtras().getString("ip");
System.out.println("adresse page2 : " + name_server);
} // public void onCreate(Bundle savedInstanceState)
public boolean onCreateOptionsMenu(Menu menu)
{
//Création d'un MenuInflater qui va permettre d'instancier un Menu XML en un objet Menu
MenuInflater inflater = getMenuInflater();
//Instanciation du menu XML spécifier en un objet Menu
inflater.inflate(R.menu.menu, menu);
return true;
}
//Méthode qui se déclenchera au clic sur un item
public boolean onOptionsItemSelected(MenuItem item)
{
//On regarde quel item a été cliqué grâce à son id et on déclenche une action
switch (item.getItemId())
{
case R.id.Internet:
//((TextView)findViewById(R.id.texte)).setText("Option");
break;
case R.id.Presentation:
Intent intent_presentenation = new Intent(mainActivityv22.this, Presentationv22.class);
startActivity(intent_presentenation);
break;
}
return true;
}
最后一个:
package Smart.Wifi.Mote.v2;
import android.os.Bundle;
...
import android.widget.ImageButton;
public class Presentationv22 extends mainActivityv22
{
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Activity Presentation §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§");
System.out.println("Constructeur classe mère");
super.onCreate(savedInstanceState);
System.out.println("Affichage");
setContentView(R.layout.presentation);
/*********************************
Appui sur Start
*********************************/
OnClickListener Start = new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
ImageButton start = (ImageButton) findViewById(R.id.ibStart);
start.setOnClickListener(Start);
/*********************************
Appui sur Stop
*********************************/
OnClickListener Stop = new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
ImageButton stop = (ImageButton) findViewById(R.id.ibStop);
stop.setOnClickListener(Stop);
}
/**********************************
Menu
**********************************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
谢谢你的帮助 :)
PS:堆栈跟踪
02-19 16:54:43.085: I/System.out(16968): Activity Presentation §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
02-19 16:54:43.085: I/System.out(16968): Constructeur classe mère
02-19 16:54:43.095: D/AndroidRuntime(16968): Shutting down VM
02-19 16:54:43.095: W/dalvikvm(16968): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
02-19 16:54:43.105: E/AndroidRuntime(16968): FATAL EXCEPTION: main
02-19 16:54:43.105: E/AndroidRuntime(16968): java.lang.RuntimeException: Unable to start activity ComponentInfo{Smart.Wifi.Mote.v2/Smart.Wifi.Mote.v2.Presentationv22}: java.lang.NullPointerException
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.os.Looper.loop(Looper.java:130)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread.main(ActivityThread.java:3701)
02-19 16:54:43.105: E/AndroidRuntime(16968): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 16:54:43.105: E/AndroidRuntime(16968): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 16:54:43.105: E/AndroidRuntime(16968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
02-19 16:54:43.105: E/AndroidRuntime(16968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
02-19 16:54:43.105: E/AndroidRuntime(16968): at dalvik.system.NativeStart.main(Native Method)
02-19 16:54:43.105: E/AndroidRuntime(16968): Caused by: java.lang.NullPointerException
02-19 16:54:43.105: E/AndroidRuntime(16968): at Smart.Wifi.Mote.v2.mainActivityv22.onCreate(mainActivityv22.java:60)
02-19 16:54:43.105: E/AndroidRuntime(16968): at Smart.Wifi.Mote.v2.Presentationv22.onCreate(Presentationv22.java:19)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 16:54:43.105: E/AndroidRuntime(16968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
02-19 16:54:43.105: E/AndroidRuntime(16968): ... 11 more