0

今、私は1つのページからAndroid上の2つのタブを含むタブホストの最初のタブにWebサービスを消費した結果を表示しようとしています。

しかし、実行後のエミュレーターは、表示されているだけyour application" has stopped unexpectedlyです。
この問題を解決する方法。

Demo.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();
    }
            }
        });
        }
    }

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

main.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 に答える