Tabhost应用程序在android / eclipse中预期关闭

时间:2012-06-18 07:00:32

标签: android eclipse android-tabhost

现在我正在尝试将一个Web服务从一个页面显示到一个tabhost的第一个选项卡,其中包含两个android选项卡。

但是模拟器在运行后才显示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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

从表面上看,这里可能存在问题:

  

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

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

  

btnFar.setOnClickListener(new View.OnClickListener(){

您确定btn不是NULL吗?