2014-06-22 47 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); 
}