Android应用程序不断崩溃

时间:2014-03-17 13:28:43

标签: java android exception nullpointerexception crash

在最近几天我尝试开发Android应用程序但没有成功,出于某种原因,我的应用程序一直在崩溃。

应用程序不断抛出我没有成功处理的exception个。{/ p>

MainActivity.java

package com.mobIce.digicoin.digitalcoin;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    private List<Coin> coins;
    public MainActivity(){
        coins = new ArrayList<Coin>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
            populateCoins();
            populateListView();
        }

    }
    private void populateCoins() {
        coins.add(new Coin("Bitcoin", "650", "4", "http://coinmarketcap.com/img/Bitcoin.png"));
        coins.add(new Coin("Dogecoin", "100", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
        coins.add(new Coin("fafaf", "8065", "400", "http://coinmarketcap.com/img/Bitcoin.png"));
    }

    private void populateListView() {
        ArrayAdapter<Coin> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.coinsListView);
        list.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

    private class MyListAdapter extends ArrayAdapter<Coin> {
        public MyListAdapter()
        {
            super(MainActivity.this, R.layout.coin_layout, coins);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View itemView = convertView;
            if(itemView == null)
                itemView = getLayoutInflater().inflate(R.layout.coin_layout, parent, false);
            //find the coin to work with.
            Coin currentCoin = coins.get(position);

            //fill the view
            try{
            TextView coinName = (TextView) itemView.findViewById(R.id.coinName);
            coinName.setText(currentCoin.getName());

            TextView coinPrice = (TextView) itemView.findViewById(R.id.coinPrice);
            coinPrice.setText(currentCoin.getPrice());

            TextView coinPercentage = (TextView) itemView.findViewById(R.id.coinPercentage);
            coinPercentage.setText(currentCoin.getPercentage());
            }
            catch (NullPointerException e){ }
            return  itemView;
        }
    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mobIce.digicoin.digitalcoin.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_main.xml:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/coinsListView"
        android:paddingLeft="30dp" />
</LinearLayout>

coin_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="coinName"
        android:id="@+id/coinName"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/coinIcon"
        android:layout_marginTop="35dp"
        android:layout_marginLeft="20dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/coinIcon"
        android:maxHeight="80dp"
        android:maxWidth="80dp"
        android:minHeight="80dp"
        android:minWidth="80dp"
        android:src="@drawable/dogecoin"
        android:longClickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:baselineAlignBottom="false"
        android:clickable="false"
        android:adjustViewBounds="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/coinPrice"
        android:layout_marginLeft="35dp"
        android:layout_alignTop="@+id/coinIcon"
        android:layout_toRightOf="@+id/coinName"
        android:layout_marginTop="25dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/coinPercentage"
        android:layout_below="@+id/coinPrice"
        android:layout_alignLeft="@+id/coinPrice"
        android:layout_alignStart="@+id/coinPrice" />

</RelativeLayout>

logcat跟踪

03-17 08:59:33.977    1107-1107/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 08:59:33.977    1107-1107/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 08:59:33.987    1107-1107/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mobIce.digicoin.digitalcoin, PID: 1107
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:47)
            at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:04:09.857    1149-1149/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:04:09.857    1149-1149/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:04:09.867    1149-1149/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mobIce.digicoin.digitalcoin, PID: 1149
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
            at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:06:36.697    1192-1192/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:06:36.697    1192-1192/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:06:36.717    1192-1192/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mobIce.digicoin.digitalcoin, PID: 1192
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
            at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:39)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:13:38.647    1239-1239/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:13:38.647    1239-1239/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:13:38.747    1239-1239/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mobIce.digicoin.digitalcoin, PID: 1239
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
            at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:13:45.127    1239-1239/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1239 SIG: 9
03-17 09:19:34.847    1285-1285/com.mobIce.digicoin.digitalcoin D/AndroidRuntime﹕ Shutting down VM
03-17 09:19:34.847    1285-1285/com.mobIce.digicoin.digitalcoin W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a38ba8)
03-17 09:19:34.857    1285-1285/com.mobIce.digicoin.digitalcoin E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mobIce.digicoin.digitalcoin, PID: 1285
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobIce.digicoin.digitalcoin/com.mobIce.digicoin.digitalcoin.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mobIce.digicoin.digitalcoin.MainActivity.populateListView(MainActivity.java:50)
            at com.mobIce.digicoin.digitalcoin.MainActivity.onCreate(MainActivity.java:37)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
03-17 09:20:11.987    1285-1285/com.mobIce.digicoin.digitalcoin I/Process﹕ Sending signal. PID: 1285 SIG: 9

1 个答案:

答案 0 :(得分:4)

您的coinsListView位于片段布局中,而不是活动布局中。您无法使用来自活动findViewById()的{​​{1}}中的populateListView()来访问它。相反,在通货膨胀后将列表视图填充移动到片段onCreate(),在刚刚膨胀的onCreateView()布局上调用findViewById()