Java.lang.RuntimeException : Unable to start activity ComponentInfo Error in Android 错误

标签 java android

为另一个问题道歉,但似乎我又陷入了死胡同。我的应用程序已经在两个独立的项目中进行了测试,它们运行良好。但是,将它们组合后,我在打开应用程序时遇到致命异常错误。我花了最后几个小时试图解决问题并研究解决方案,但到目前为止没有任何效果。请记住,我是编程新手,不确定问题出在哪里。

也为这么多代码道歉。我确实考虑过只发布错误日志,但不确定代码或 xml 文件是否有用。代码确实工作正常,我已经在个别项目中对其进行了测试,但是在仔细组合代码之后,由于某种原因,它出现了这个错误。谢谢!!!

编辑:解决了评论中的问题。可能会很快删除问题。感谢看过的人:)

错误日志

03-20 02:08:54.761: E/AndroidRuntime(12530): FATAL EXCEPTION: main
03-20 02:08:54.761: E/AndroidRuntime(12530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.motionsense/com.example.motionsense.MainActivity}: java.lang.NullPointerException
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.os.Looper.loop(Looper.java:158)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread.main(ActivityThread.java:5789)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at java.lang.reflect.Method.invoke(Method.java:525)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at dalvik.system.NativeStart.main(Native Method)
03-20 02:08:54.761: E/AndroidRuntime(12530): Caused by: java.lang.NullPointerException
03-20 02:08:54.761: E/AndroidRuntime(12530):    at com.example.motionsense.SaveTimer.<init>(SaveTimer.java:18)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at com.example.motionsense.MainActivity.onCreate(MainActivity.java:66)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.Activity.performCreate(Activity.java:5195)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
03-20 02:08:54.761: E/AndroidRuntime(12530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
03-20 02:08:54.761: E/AndroidRuntime(12530):    ... 11 more
03-20 02:08:55.963: D/Process(12530): killProcess, pid=12530
03-20 02:11:57.106: W/asset(12774): Copying FileAsset 0x69a900f8 (zip:/data/app/com.example.motionsense-2.apk:/resources.arsc) to buffer size 2312 to make it aligned.
03-20 02:11:57.156: W/dalvikvm(12774): threadid=1: thread exiting with uncaught exception (group=0x4169d970)

程序

package com.example.motionsense;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity implements SensorEventListener {

 //private FragmentActivity mClass;

 private List<String> item = null;
 private List<String> path = null;
 private String root;
 private TextView myPath;

 Timer timer = new Timer();

 File myFile;
 FileOutputStream fOut;
 OutputStreamWriter myOutWriter;
 BufferedWriter myBufferedWriter;
 PrintWriter myPrintWriter;
 ArrayList<String> motionData;

 float[] acceleration = new float[3];
 float[] rotationRate = new float[3];
 float[] magneticField = new float[3];

 boolean startScan = false;
 boolean isFirstSet = true;

 private SensorManager sensorManager;
 private long currentTime;
 private long startTime;

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

        //get file directory
        myPath = (TextView)findViewById(R.id.path);
        //root = Environment.getExternalStorageDirectory().getPath();
        root = getFilesDir().getPath();

        //set save data timer
        SaveTimer savetimer = new SaveTimer(motionData, this);
        timer.schedule(savetimer, 1000, 60000);

        getDir(root);
    }

    private void getDir(String dirPath)
    {

     myPath.setText("Location: " + dirPath);
     item = new ArrayList<String>();
     path = new ArrayList<String>();
     File f = new File(dirPath);
     File[] files = f.listFiles();

     if(!dirPath.equals(root))
     {
      item.add(root);
      path.add(root);
      item.add("../");
      path.add(f.getParent()); 
     }

     for(int i=0; i < files.length; i++)
     {
      File file = files[i];

      if(!file.isHidden() && file.canRead()){
       path.add(file.getPath());
          if(file.isDirectory()){
           item.add(file.getName() + "/");
          }else{
           item.add(file.getName());
          }
      } 
     }

     ArrayAdapter<String> fileList =
       new ArrayAdapter<String>(this, R.layout.row, item);
     setListAdapter(fileList); 
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub


  File file = new File(path.get(position));

  if (file.isDirectory())
  {
   if(file.canRead()){
    getDir(path.get(position));
   }else{
    new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "] folder can't be read!")
     .setPositiveButton("OK", null).show(); 
   } 
  }else {
   new AlertDialog.Builder(this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("[" + file.getName() + "]")
     .setPositiveButton("OK", null).show();

    }
 }



    @Override
    public void onSensorChanged (SensorEvent event) {
        // 

                if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                    acceleration[0] = event.values[0];
                    acceleration[1] = event.values[1];
                    acceleration[2] = event.values[2];
                }

                if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
                    rotationRate[0] = event.values[0];
                    rotationRate[1] = event.values[1];
                    rotationRate[2] = event.values[2];
                }

                if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                    magneticField[0] = event.values[0];
                    magneticField[1] = event.values[1];
                    magneticField[2] = event.values[2];
                }

                if (isFirstSet) {
                    startTime = System.currentTimeMillis();
                    isFirstSet = false;
                }

                currentTime = System.currentTimeMillis();
                record();

    }



    private void record() {

        motionData.add("Acceleration= " + "X: " + acceleration[0] + " " + "Y: " + acceleration[1] + " " + "Z: " + acceleration[2] + "\n "
                    + "GyroScope= " + " " + "X: " + rotationRate[0] + " " + "Y: " + rotationRate[1] + " " + "Z: " + rotationRate[2] + "\n "
                    + "Magnetic Field= " + " " + "X: " + magneticField[0] + " " + "Y: " + magneticField[1] + " " + "Z: " + magneticField[2] + "\n\n");
}

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }


}

XML 文件 1:Activity_Main.xml

<LinearLayout 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" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/path"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="No Data"
        />

</LinearLayout>

XML 文件 2:行.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowtext"
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:textSize="25sp" />

最佳答案

SaveTimer savetimer = new SaveTimer(motionData, this); motionDatanull,你还没初始化?

关于Java.lang.RuntimeException : Unable to start activity ComponentInfo Error in Android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522738/

相关文章:

java - 类型转换通用列表

要求Android服务每十分钟持续运行一次

android - 无法在 Android 12 中使用 onCapabilitiesChanged 获取 WIFI SSID

javascript - WebView Html 固定标题滚动表

java - 无法将RxJava dep添加到gradle

java - 在 Java 中监控 ObjectOutputStream

java - 部分用户出现安全异常 : Attempted to to open a sandboxed jar as a Trusted-Library

Java HashSet 以奇怪的顺序显示列表,总是从 3 开始

android - android native 应用程序的默认 User-Agent 字符串是什么?

android - Amazon IAP 不调用 onPurchaseResponse