2014-06-22 21 views
-1

我是android应用程序开发的新手,我正在开发一个android应用程序以读取包含频谱数据的.txt文件。该文件包含整数和空格,因此读取每行后,代码会修剪该字符串以删除空格,然后将包含字符串的字符串转换为整数。整数表示Y坐标值,行号表示X坐标值。我必须绘制散点图,并且我正在使用Achartengine。当我尝试在模拟器上运行应用程序时,应用程序崩溃。日志猫给出了这样的信息阅读Android应用程序的文件中的Java空指针异常

06-22 09:08:58.114: E/AndroidRuntime(1015): FATAL EXCEPTION: main 
06-22 09:08:58.114: E/AndroidRuntime(1015): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spec1readertest/com.example.spec1readertest.MainActivity}: java.lang.NullPointerException 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.os.Looper.loop(Looper.java:137) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at java.lang.reflect.Method.invoke(Method.java:525) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at dalvik.system.NativeStart.main(Native Method) 
06-22 09:08:58.114: E/AndroidRuntime(1015): Caused by: java.lang.NullPointerException 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at com.example.spec1readertest.MainActivity.onStart(MainActivity.java:89) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.Activity.performStart(Activity.java:5143) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 
06-22 09:08:58.114: E/AndroidRuntime(1015):  ... 11 more 

这是主要的activity.java文件

package com.example.spec1readertest; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import org.achartengine.GraphicalView; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    ScatterGraphPlot scatter; 
    GraphicalView view; 
    LinearLayout layout; 
    EditText filename; 
    String filepath; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     filename = (EditText) findViewById(R.id.filename); 

     // Initialize loadspec btn 
     Button loadspec = (Button) findViewById(R.id.button2); 
     loadspec.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       // filename input 
       filepath =filename.getText().toString(); 

       //set file path 
        try { 
         filepath = "/sdcard/"+filepath+".txt"; 
        BufferedReader br = new BufferedReader(new FileReader(filepath)); 
         String specData = ""; 
         int specVal; 
         int channelVal=1; 
         while ((specData = br.readLine()) != null) { 

          // Method for converting the text read from file to an integer 
          specVal=scatter.StringToNumber(specData); 
          // adding points to XYseries 
          scatter.addPoints(channelVal, specVal); 
          //repainting the view; 
          view.repaint(); 
          //setting the specData string to an empty string 
          specData=""; 
          //incrementing the channel value 
          channelVal++; 

         } 
        } 

        catch (IOException e) { 

         Toast.makeText(getBaseContext(),"file not found",Toast.LENGTH_SHORT).show(); 
        } 

       } 

     }); 

     // initialize exit button 
     Button exit = (Button) findViewById(R.id.button3); 
     exit.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       finish(); // exits the application 

      } 
     }); 
    } 

    protected void onStart() { 
     super.onStart(); 
     view = scatter.getView(this); 
     layout = (LinearLayout) findViewById(R.id.chart); 
     layout.addView(view); 

    } 

} 

这是我使用的绘制图

public ScatterGraphPlot() { 
     // adding series to xymultiple series dataset 
     dataset.addSeries(series1); 

     // customizing the points on scatter graph 
     renderer.setColor(Color.GREEN); 
     renderer.setPointStyle(PointStyle.CIRCLE); 
     renderer.setFillPoints(true); 

     // customizing the entire graph 
     mRenderer.addSeriesRenderer(renderer); 
     mRenderer.setAxesColor(Color.WHITE); 
     mRenderer.setBackgroundColor(Color.BLACK); 
     mRenderer.setApplyBackgroundColor(true); 
     mRenderer.setZoomButtonsVisible(true); 

    } 

    public GraphicalView getView(Context context) { 

     //returning the graphical view to the main activity 
     view = ChartFactory.getScatterChartView(context, dataset, mRenderer); 
     return view; 

    } 

    public void addPoints(int x, int y) { 

     chval = x; 
     specval = y; 

     //add points to series1 
     series1.add(chval, specval); 
    } 

    //this method converts string to a number 
    int StringToNumber(String str) { 
     NumConvStr = str; 
     NumConvStr=NumConvStr.trim(); 
     try { 
      RetNum = Integer.parseInt(NumConvStr); 
      return RetNum; 
     } 
     //ignore catch block :assuming file to be error free 
     catch (NumberFormatException e) { 
      return 0; 
     } 
    } 
} 

的的ScatterGraphPlot.java文件我的应用程序的XML文件是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="${packageName}.${activityClass}" > 

    <Button 
     android:id="@+id/button3" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button2" 
     android:layout_alignBottom="@+id/button2" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:text="@string/Close" /> 

    <EditText 
     android:id="@+id/filename" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="20dp" 
     android:ems="10" 
     android:hint="@string/filename" /> 

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/filename" 
     android:layout_marginLeft="22dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/loadspec" /> 

    <LinearLayout 
     android:id="@+id/chart" 
     android:layout_width="match_parent" 
     android:layout_height="250dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="26dp" 
     android:orientation="vertical" > 
    </LinearLayout> 

</RelativeLayout> 

我希望有人会帮我解决这个问题。

+0

看看'MainActivity.java'行89就像你的堆栈跟踪所告知的那样。或者至少告诉我们它在代码中的哪一行;)看起来像分散或布局可能为空。取决于第89行的位置。 –

+1

*方法*代码太多,请创建[SSCCE](http://sscce.org)。 –

+0

+1 Big Fan的SSCCE.org;)@BaummitAugen –

回答

1

检查你的堆栈跟踪

Caused by: java.lang.NullPointerException 
    at com.example.spec1readertest.MainActivity.onStart(MainActivity.java:89) 

后,我觉得现场scatter从未assigend的值。这将是你NPE的来源。 以下更改应该做的伎俩。

protected void onStart() { 
    super.onStart(); 
    scatter = // initialize your scatter here first! 
    view = scatter.getView(this); 
    layout = (LinearLayout) findViewById(R.id.chart); 
    layout.addView(view); 
}