当前位置: 首页 > 知识库问答 >
问题:

android意图空指针异常

戚令秋
2023-03-14
public class AreaURL extends Activity implements View.OnClickListener{

Button button1;
private String petrolPriceURL = "http://www.petrolprices.com/feeds/averages.xml?search_type=town&search_value=glasgow";

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userurldisplay);

    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
public void onClick(View v) {

new AlertDialog.Builder(Area.this)
.setTitle("Confirm")
.setMessage("Is this the XML feed you require?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // continue
            TestMethod();
        }
    })
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do nothing
    }
 })
.setIcon(android.R.drawable.ic_dialog_alert)
 .show();
public void TestMethod(){
    EditText mEdit;
    EditText mEdit2;

    String newFirstString = petrolPriceURL.substring(0,59);
    String newSecondString = petrolPriceURL.substring(63,77);

    mEdit   = (EditText)findViewById(R.id.editText1);
    mEdit.getText().toString();

    mEdit2   = (EditText)findViewById(R.id.editText2);
    mEdit2.getText().toString();

    String newThirdString = newFirstString + mEdit.getText().toString();
    String newFourthString = newSecondString + mEdit2.getText().toString();
    String newFifthString = newThirdString + newFourthString;

    Intent intent=new Intent(this,PetrolPriceActivity.class);
    intent.putExtra("URLString",newFifthString);
    startActivity(intent);
}

}
Intent intent=new Intent(this,PetrolPriceActivity.class);
    intent.putExtra("URLString",newFifthString);
    startActivity(intent);
}

}
   response = (TextView)findViewById(R.id.textView2);
    response.setText(newFifthString);

08-0712:11:44.453:I/Choreographer(1257):跳过126帧!
应用程序可能在主线程上做了太多工作。08-07 12:11:46.313:I/Choreographer(1257):跳过了131帧!
应用程序可能在主线程上做了太多工作。08-07 12:11:48.033:I/Choreographer(1257):跳过了33个帧!
应用程序可能在主线程上做了太多的工作。08-07 12:11:51.753:D/DALVIKVM(1257):GC_FOR_ALLOC释放137K,6%空闲3938K/4152K,暂停146ms,总计146ms08-07 12:11:52.003:D/AndroidRuntime(1257):关闭VM08-07 12:11:52.003:W/DALVIKVM(1257):thread=1:线程退出未捕获异常(组=0xB3A96BA8)08-07 12:11:52.063:E/AndroidRuntime(1257):致命异常:main 08-07 12:11:52.063:E/AndroidRuntime(1257):main):进程:org.me.myandroidstuff,PID:1257 08-07 12:11:52.063:e/androidruntime(1257):java.lang.runtimeException:无法启动活动ComponentInfo{org.me.myandroidstuft/org.me.myandroidstuff.petrolpriceactivity}:java.lang.nullpointerException 08-07 12:11:52.063:e/androidruntime(1257):at eLaunchActivity(ActivityThread.java:2245)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.app.ActivityThread.Access$800(ActivityThread.java:135)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.app.ActivityThread$H.HandleMessage(ActivityThread.java:1196)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.os.handler.dispatchMessage(handler.java:102)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.os.looper.loop(looper.java:136)08-07)08-07 12:11:52.063:e/AndroidRuntime(1257):at java.lang.reflect.Method.invokenative(本机方法)08-07 12:11:52.063:e/AndroidRuntime(1257):at java.lang.reflect.Method.invoke(515):at java.lang.reflect.Method.invoke(1257):at Method.java:515)08-07 12:11:52.063:e/AndroidRuntime(1257):at goteinit.main(zygoteinit.java:595)08-07 12:11:52.063:e/AndroidRuntime(1257):at dalvik.system.nativeStart.main(本机方法)08-07 12:11:52.063:e/AndroidRuntime(1257):by:java.lang.NullPointer异常08-07 12:11:52.063:e/AndroidRuntime(1257):at org.me.myAndroidStuff.petrolpriceActivity.onCreate(petrolpriceActivity.java:53)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.app.activity.performCreate(activity.java:53)08-07 12:11:52.063:e/AndroidRuntime(1257):at Android.app.activity.5231):at ythread.performLaunchActivity(activitythread.java:2159)08-07 12:11:52.063:e/AndroidRuntime(1257):...11更多

我的xml布局

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:text="@string/thank_you" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_toRightOf="@+id/textView2"
    android:background="#CCCCCC"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_toRightOf="@+id/textView2"
    android:background="#CCCCCC"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="214dp"
    android:layout_height="wrap_content"
    android:text="@string/OK" />

这是我的PetrolPriceActivity课

package org.me.myandroidstuff;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class PetrolPriceActivity extends Activity 
{
private TextView response;
private TextView errorText;
private String result;
private String petrolPriceURL;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{

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

    Bundle extras = getIntent().getExtras();
    if(extras!=null){
        petrolPriceURL =extras.getString("URLString");

    }
    // Get the TextView object on which to display the results
    response = (TextView)findViewById(R.id.title);
    try
    {
        // Get the data from the RSS stream as a string
        result =  petrolPriceString(petrolPriceURL);

        // Do some processing of the data to get the individual parts of the RSS stream
        // At some point put this processing into a separate thread of execution
        // Display the string in the TextView object just to demonstrate this capability
        // This will need to be removed at some point
        response.setText(result);
    }
    catch(IOException ae)
    {
        // Handle error
        response.setText("Error");
        // Add error info to log for diagnostics
        errorText.setText(ae.toString());
    } 

}

// End of onCreate

// Method to handle the reading of the data from the RSS stream
private static String petrolPriceString(String urlString)throws IOException
{
    String result = "";
    InputStream anInStream = null;
    int response = -1;
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    // Check that the connection can be opened
    if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");
    try
    {
        // Open connection
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        // Check that connection is Ok
        if (response == HttpURLConnection.HTTP_OK)
        {
            // Connection is OK so open a reader 
            anInStream = httpConn.getInputStream();
            InputStreamReader in= new InputStreamReader(anInStream);
            BufferedReader bin= new BufferedReader(in);

            // Read in the data from the RSS stream
            String line = new String();
            while (( (line = bin.readLine())) != null)
            {
                result = result + "\n" + line;
            }
        }
    }
    catch (Exception ex)
    {
            throw new IOException("Error connecting");
    }

    // Return result as a string for further processing
    return result;
}
// End of petrolPriceString
// End of Activity class
}

共有1个答案

魏君博
2023-03-14

错误是

errorText.setText(ae.toString());

您没有初始化该变量。

@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    //////////////////////////////////////////////////////Solution
    errorText = (TextView)findViewById(R.id.yourTextView);

    Bundle extras = getIntent().getExtras();
    if(extras!=null){
        petrolPriceURL =extras.getString("URLString");

    }
    // Get the TextView object on which to display the results
    response = (TextView)findViewById(R.id.title);
    try
    {
        // Get the data from the RSS stream as a string
        result =  petrolPriceString(petrolPriceURL);

        // Do some processing of the data to get the individual parts of the RSS stream
        // At some point put this processing into a separate thread of execution
        // Display the string in the TextView object just to demonstrate this capability
        // This will need to be removed at some point
        response.setText(result);
    }
    catch(IOException ae)
    {
        // Handle error
        response.setText("Error");
        // Add error info to log for diagnostics
        errorText.setText(ae.toString()); //////////This variable is not intialized.
    } 

}

 类似资料:
  • 有时,当我试图创建模糊位图时,我会遇到“空指针异常”。 发生在这段代码中(我最近开始捕获异常,因此至少它不会使应用程序崩溃): 请参考这些图片以获得更多关于我传递给“CreateBitmap”方法的参数的细节: 以下是扩展的参数: 完全异常: 异常{Java。Lang.NullPointerException:类型为“Java”的异常。引发了“Lang.NullPointerException”。

  • 问题内容: 有可能这可能是一个双重问题。我将String变量初始化为null。我可能会或可能不会使用一个值更新它。现在我想检查此变量是否不等于null以及我尝试执行的操作是否会得到null指针异常。空指针异常,因为它代价高昂。是否有任何有效的解决方法.TIA 问题答案: 如果您使用 你 不会 得到。 我怀疑你在做什么: 这是因为null 而引发,而不是因为null。 如果仍然无法解释,请发布您用于

  • 我已经更新了我的项目中的一些依赖关系之后,我的Hibernate配置类显示Nullpointerx的。 我将SpringDataJPA存储库与hibernate一起使用,已经超过24小时了,仍然没有找到任何关于小问题的适当解决方案。 我已经尝试过的一些解决方案:- 使用@bean(name=“entityManagerFactory”)提供bean名称 我面临的问题 波姆。xml文件 配置类 db

  • 我一直在youtube上学习android的自定义视图等等。在我尝试做一个表面视图(相当简单的描述在这个视频)。 我所做的事情与视频中所显示的几乎是同步的。

  • 当我试图从DB查询结果时,我得到一个空指针异常。以下是错误: 这是错误产生的函数。 如果我启动应用程序信息服务(=new ApplicationInfoService()),我会得到一个指向我的服务类的空指针错误,在这里进行查询 错误转到createQuery行,如果我打印出EntityManager,它将返回“null”。为什么我的Entitymanager不能初始化。我在我的登录系统中使用了几

  • 问题内容: 我有一个非常简单的应用程序,它只是一个带有选项卡视图的活动。 我已经初始化并将所有内容强制转换为应有的值,但是不断出现空指针错误,该错误始终链接回 tabHost.setup(); 我正在使用android studio,并且是java的新手。这个问题在这里已经问了很多,但所有答案都只是说要包含setup(),而我已经做到了。 这是我的.java文件: 我的代码和一些在线教程之间的唯一