0

现在我正在尝试显示从一个页面到标签主机的第一个标签的消费结果,该标签包含 android 上的两个标签。

但是模拟器运行后只是显示your application" has stopped unexpectedly
如何解决这个问题呢。

演示.webserviceactivity.java

public class Demo_webserviceActivity extends Activity
{
/** Called when the activity is first created. */

   private static String NAMESPACE = "http://tempuri.org/";
   private static String METHOD_NAME = "FahrenheitToCelsius";
   private static String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
   private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";


EditText txtFar;
Button btnFar;
SharedPreferences sharedPreferences;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    txtFar=(EditText)findViewById(R.id.editText1);
    btnFar=(Button)findViewById(R.id.btnFar);

    btnFar.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v){
   SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

   request.addProperty("Fahrenheit",txtFar.getText().toString());

   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

   envelope.setOutputSoapObject(request);
   envelope.dotNet = true;

   try
   {
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

       androidHttpTransport.call(SOAP_ACTION, envelope);

       SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

if(result != null)
{
    sharedPreferences=PreferenceManager.getDefaultSharedPreferences(Demo_webserviceActivity.this);
    Editor editor1 = sharedPreferences.edit();
    editor1.remove("answer");
    editor1.commit();

    sharedPreferences=PreferenceManager.getDefaultSharedPreferences(Demo_webserviceActivity.this);
    Editor editor = sharedPreferences.edit();
    Log.i("set value",""+txtFar.getText().toString());
    editor.putString("answer",txtFar.getText().toString());
    editor.commit();
   }
   else
   {
    Toast.makeText(getApplicationContext(), "oops! empty!...",        Toast.LENGTH_SHORT).show();
    }
   }    
       catch(Exception e)
       {
        e.printStackTrace();
    }
            }
        });
        }
    }

日志猫:

06-18 12:22:59.902: D/AndroidRuntime(482): Shutting down VM
06-18 12:22:59.922: W/dalvikvm(482): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-18 12:22:59.932: E/AndroidRuntime(482): FATAL EXCEPTION: main
06-18 12:22:59.932: E/AndroidRuntime(482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.webser/com.demo.webser.Demo_webserviceActivity}: java.lang.NullPointerException
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.os.Looper.loop(Looper.java:123)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-18 12:22:59.932: E/AndroidRuntime(482):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:22:59.932: E/AndroidRuntime(482):  at java.lang.reflect.Method.invoke(Method.java:521)
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-18 12:22:59.932: E/AndroidRuntime(482):  at dalvik.system.NativeStart.main(Native Method)
06-18 12:22:59.932: E/AndroidRuntime(482): Caused by: java.lang.NullPointerException
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.demo.webser.Demo_webserviceActivity.onCreate(Demo_webserviceActivity.java:45)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-18 12:22:59.932: E/AndroidRuntime(482):  ... 11 more

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >



<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnFar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get result" />

</LinearLayou>

main1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

修复后的 Logcat

06-18 12:22:59.902: D/AndroidRuntime(482): Shutting down VM
06-18 12:22:59.922: W/dalvikvm(482): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-18 12:22:59.932: E/AndroidRuntime(482): FATAL EXCEPTION: main
06-18 12:22:59.932: E/AndroidRuntime(482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.webser/com.demo.webser.Demo_webserviceActivity}: java.lang.NullPointerException
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.os.Looper.loop(Looper.java:123)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-18 12:22:59.932: E/AndroidRuntime(482):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:22:59.932: E/AndroidRuntime(482):  at java.lang.reflect.Method.invoke(Method.java:521)
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-18 12:22:59.932: E/AndroidRuntime(482):  at dalvik.system.NativeStart.main(Native Method)
06-18 12:22:59.932: E/AndroidRuntime(482): Caused by: java.lang.NullPointerException
06-18 12:22:59.932: E/AndroidRuntime(482):  at com.demo.webser.Demo_webserviceActivity.onCreate(Demo_webserviceActivity.java:45)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-18 12:22:59.932: E/AndroidRuntime(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-18 12:22:59.932: E/AndroidRuntime(482):  ... 11 more
06-18 12:52:27.612: W/KeyCharacterMap(509): No keyboard for id 0
06-18 12:52:27.612: W/KeyCharacterMap(509): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-18 12:52:31.523: I/set value(509): 56

谢谢你的帮助!

4

1 回答 1

2

从外观上看,问题可能出在此处:

06-18 12:22:59.932:E/AndroidRuntime(482):引起:java.lang.NullPointerException 06-18 12:22:59.932:E/AndroidRuntime(482):在 com.demo.webser.Demo_webserviceActivity.onCreate (Demo_webserviceActivity.java:45)

看看你的代码,我猜第 45 行是(因为没有给出行号):

btnFar.setOnClickListener(new View.OnClickListener() {

你确定那btn不是NULL吗?

于 2012-06-18T07:06:39.287 回答