gpt4 book ai didi

android - 在 android eclipse 上切换页面时出错

转载 作者:行者123 更新时间:2023-11-30 04:11:26 24 4
gpt4 key购买 nike

   public void onClick(View v)
{
fun_demo();
Intent myIntent = new Intent(v.getContext(), Activity1.class);
startActivityForResult(myIntent, 0);
}

假设上面的函数“fun_demo()”将在同一屏幕上单击按钮时通过 TextView 在同一屏幕上返回一些值。

这里我需要在第二个屏幕上显示可返回值。所以我将这些行添加到我的 firstscreen.java

Intent myIntent = new Intent(v.getContext(), Activity1.class);
startActivityForResult(myIntent, 0);

在函数fun_demo()中,这段代码对吗?

注意:fun_demo()的代码写在屏幕一上。

供引用的FirstscreensActivity.java

package com.android.button.web;

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.app.*;
import android.content.Intent;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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

private static final String NAMESPACE = "http://tempuri.org/";
private static final String METHOD_NAME = "HelloWorld";
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String URL = "http://122.248.240.105:93/Student.asmx?WSDL";
TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.textView_222);

Button btn = (Button)findViewById(R.id.but);
btn.setOnClickListener(btnListener);
}

public void fun_demo()
{
TextView tv = (TextView) findViewById(R.id.textView_222);
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("str","");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
Object result = envelope.getResponse();
System.out.println("Result : " + result.toString());
((TextView) findViewById (R.id.textView_222)).setText(""+result.toString());
} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById (R.id.textView_222)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage());
} }

private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
fun_demo();
Intent myIntent = new Intent(v.getContext(), Activity1.class);
startActivityForResult(myIntent, 0);
}
};
}

这个概念其实就是要在android eclipse上消费一个web service

这是我的 logcat 以供引用。

LOGCAT

05-29 18:22:04.455: W/System.err(1300):     at android.os.Looper.loop(Looper.java:123)
05-29 18:22:04.465: W/System.err(1300): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-29 18:22:04.465: W/System.err(1300): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 18:22:04.465: W/System.err(1300): at java.lang.reflect.Method.invoke(Method.java:521)
05-29 18:22:04.465: W/System.err(1300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-29 18:22:04.465: W/System.err(1300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-29 18:22:04.465: W/System.err(1300): at dalvik.system.NativeStart.main(Native Method)
05-29 18:22:04.465: D/AndroidRuntime(1300): Shutting down VM
05-29 18:22:04.465: W/dalvikvm(1300): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-29 18:22:04.476: E/AndroidRuntime(1300): FATAL EXCEPTION: main
05-29 18:22:04.476: E/AndroidRuntime(1300): java.lang.NullPointerException
05-29 18:22:04.476: E/AndroidRuntime(1300): at com.android.button.web.Tesing_webserviceActivity.CallWebService(Tesing_webserviceActivity.java:55)
05-29 18:22:04.476: E/AndroidRuntime(1300): at com.android.button.web.Tesing_webserviceActivity$1.onClick(Tesing_webserviceActivity.java:62)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.view.View.performClick(View.java:2408)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.view.View$PerformClick.run(View.java:8816)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.os.Handler.handleCallback(Handler.java:587)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.os.Looper.loop(Looper.java:123)
05-29 18:22:04.476: E/AndroidRuntime(1300): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-29 18:22:04.476: E/AndroidRuntime(1300): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 18:22:04.476: E/AndroidRuntime(1300): at java.lang.reflect.Method.invoke(Method.java:521)
05-29 18:22:04.476: E/AndroidRuntime(1300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-29 18:22:04.476: E/AndroidRuntime(1300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-29 18:22:04.476: E/AndroidRuntime(1300): at dalvik.system.NativeStart.main(Native Method)

非常感谢!..

最佳答案

Intent myIntent = new Intent(v.getContext(), Activity1.class);

此行由于未初始化 Activity 而产生问题

关于android - 在 android eclipse 上切换页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10799652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com