应用程序很少在setContentView上崩溃

时间:2014-08-23 08:56:40

标签: android

我有来自另一个ListActivity的ListActivity Intent,大约1倍到10倍的程序崩溃Intent活动,当我记录问题是setContentView

public class MessageActivity extends ListActivity {
 private ListView list;

 //...

  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_message);
     list = getListView();

  //...

  }
}

activity_message XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@id/activity_welcome_l1"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" >
<LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@color/white"
          android:orientation="vertical" android:gravity="center">
<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:layout_gravity="top">
    <include  layout="@layout/header4" />

</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8">
    <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:transcriptMode="alwaysScroll"
            android:descendantFocusability="blocksDescendants"
            android:stackFromBottom="true"
            >
    </ListView>
</LinearLayout>
<LinearLayout
        android:id="@+id/bottom_write_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/message_bar"
        android:layout_gravity="bottom" android:orientation="vertical">

    <include  layout="@layout/footer1" android:layout_width="fill_parent"
              android:layout_height="fill_parent"/>
</LinearLayout>
</LinearLayout>
<include  layout="@layout/draver_menu" />
</FrameLayout>

RuntimeException:

08-23 13:32:26.910: ERROR/AndroidRuntime(3737): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.in.ss/ir.in.ss.pages.sms_chat.MessageActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3714)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:682)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:619)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:211)
    at android.app.Activity.setContentView(Activity.java:1657)
    at ir.in.ss.pages.sms_chat.MessageActivity.onCreate(MessageActivity.java:108)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630)
    ... 11 more
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    ... 26 more
    Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    at android.content.res.Resources.loadDrawable(Resources.java:1709)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    at android.view.View.<init>(View.java:1951)
    at android.widget.TextView.<init>(TextView.java:350)
    at android.widget.TextView.<init>(TextView.java:343)
    ... 29 more

主要活动:

public class MainActivity extends ListActivity {
private Activity A;
private ListView list;
private LazyAdapter adapter;
private LinearLayout search_layer;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.activity_main);
        this.A = this;
        list = getListView();
        adapter = new LazyAdapter(A, R.layout.list_row, R.layout.layout_one, H.BoxInfo);
        setListAdapter(adapter);

        ...
        Button search_button = (Button) A.findViewById(R.id.activity_main_button_search);
        search_layer = (LinearLayout) A.findViewById(R.id.activity_main_search_layer);
        search_layer.setVisibility(View.GONE);
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Vibrator vibe = (Vibrator) A.getSystemService(Context.VIBRATOR_SERVICE);
                vibe.vibrate(50);
                if (search_layer.getVisibility() == View.GONE) {
                    search_layer.setVisibility(View.VISIBLE);
                    search_text.requestFocus();

                } else {
                    search_layer.setVisibility(View.GONE);
                    search_text.setText("");
                    imm.hideSoftInputFromWindow(search_text.getWindowToken(), 0);
                }
            }
        });


        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
                Vibrator vibe = (Vibrator) A.getSystemService(Context.VIBRATOR_SERVICE);
                vibe.vibrate(50);
                BoxInfoObject Box = adapter.getItem(position);
                inbox_init QA = new inbox_init(A, adapter, Box);
                inboxQuickAction quickAction = QA.initialize(Box);
                quickAction.show(view);
                return true;
            }
        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                BoxInfoObject Box = adapter.getItem(position);
                Intent intent = new Intent(A, MessageActivity.class);
                intent.putExtra(MessageActivity.KEY_POSITION, H.BoxInfo.indexOf(Box) + "");
                intent.putStringArrayListExtra(MessageActivity.KEY_NEW_MESSAGE_OPNS,
                        Box.getOPNs());
                intent.putStringArrayListExtra(MessageActivity.KEY_NEW_MESSAGE_PNS,
                        Box.getPNs());
                A.startActivity(intent);
            }
        });

    } catch (Exception e3) {
        Log.i("H", "MainActivity.Exception e3******************************");
        Toast.makeText(this, this.getString(R.string.error1) +
                "002" + this.getString(R.string.error2)
                , Toast.LENGTH_LONG).show();
        onBackPressed();
    }
}


@Override
public void onBackPressed() {
    if (search_layer.getVisibility() == View.VISIBLE) {
        search_layer.setVisibility(View.GONE);
    } else {
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    startActivity(i);
    }
}

@Override
public void onPause() {
    Log.i("H", "MainActivity.onResume");
    super.onPause();
}

@Override
protected void onResume() {
    Log.i("H", "MainActivity.onResume");
    super.onResume();
}

@Override
protected void onRestart() {
    super.onRestart();
    Log.i("H", "MainActivity.onRestart");
}

@Override
protected void onStart() {
    super.onStart();
    Log.i("H", "MainActivity.onStart");
}

@Override
protected void onStop() {
    super.onStop();
    Log.i("H", "MainActivity.onStop");
}

@Override
public void onDestroy() {
    Log.i("H", "MainActivity.onResume");
    super.onDestroy();
}

}

任何建议?

2 个答案:

答案 0 :(得分:0)

使用ListActivity时不应使用setContentView 如果你需要,你应该使用Activity并在其布局中使用ListView小部件

public class MainActivity extends Activity {
private Context A;
private ListView list;
private LazyAdapter adapter;
private LinearLayout search_layer;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
    setContentView(R.layout.activity_main);
    this.A = this;
    list = (ListView) findViewById(R.id.listView);
    adapter = new LazyAdapter(this, R.layout.list_row, R.layout.layout_one, H.BoxInfo);
    setListAdapter(adapter);

    ...
    Button search_button = (Button) A.findViewById(R.id.activity_main_button_search);
    search_layer = (LinearLayout) A.findViewById(R.id.activity_main_search_layer);
    search_layer.setVisibility(View.GONE);
    search_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Vibrator vibe = (Vibrator) A.getSystemService(Context.VIBRATOR_SERVICE);
            vibe.vibrate(50);
            if (search_layer.getVisibility() == View.GONE) {
                search_layer.setVisibility(View.VISIBLE);
                search_text.requestFocus();

            } else {
                search_layer.setVisibility(View.GONE);
                search_text.setText("");
                imm.hideSoftInputFromWindow(search_text.getWindowToken(), 0);
            }
        }
    });


    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
            Vibrator vibe = (Vibrator) A.getSystemService(Context.VIBRATOR_SERVICE);
            vibe.vibrate(50);
            BoxInfoObject Box = adapter.getItem(position);
            inbox_init QA = new inbox_init(A, adapter, Box);
            inboxQuickAction quickAction = QA.initialize(Box);
            quickAction.show(view);
            return true;
        }
    });

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            BoxInfoObject Box = adapter.getItem(position);
            Intent intent = new Intent(A, MessageActivity.class);
            intent.putExtra(MessageActivity.KEY_POSITION, H.BoxInfo.indexOf(Box) + "");
            intent.putStringArrayListExtra(MessageActivity.KEY_NEW_MESSAGE_OPNS,
                    Box.getOPNs());
            intent.putStringArrayListExtra(MessageActivity.KEY_NEW_MESSAGE_PNS,
                    Box.getPNs());
            A.startActivity(intent);
        }
    });

} catch (Exception e3) {
    Log.i("H", "MainActivity.Exception e3******************************");
    Toast.makeText(this, this.getString(R.string.error1) +
            "002" + this.getString(R.string.error2)
            , Toast.LENGTH_LONG).show();
    onBackPressed();
}
}


@Override
public void onBackPressed() {
if (search_layer.getVisibility() == View.VISIBLE) {
    search_layer.setVisibility(View.GONE);
} else {
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
}

@Override
public void onPause() {
Log.i("H", "MainActivity.onResume");
super.onPause();
}

@Override
protected void onResume() {
Log.i("H", "MainActivity.onResume");
super.onResume();
}

@Override
protected void onRestart() {
super.onRestart();
Log.i("H", "MainActivity.onRestart");
}

@Override
protected void onStart() {
super.onStart();
Log.i("H", "MainActivity.onStart");
}

@Override
protected void onStop() {
super.onStop();
Log.i("H", "MainActivity.onStop");
}

@Override
public void onDestroy() {
Log.i("H", "MainActivity.onResume");
super.onDestroy();
}
}  

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="wrap_content"
android:orientation="vertical"
android:background="@drawable/ic_launcher" >

<ListView 
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

答案 1 :(得分:0)

您的代码未达到search_layer = (LinearLayout) A.findViewById(R.id.activity_main_search_layer);所以在onBackPressed()中,search_layer为空,这就是您获得NullPointerException的原因,您需要弄清楚为什么会抛出e3。 因为你有OutOfMemoryException,我猜你正在使用的图像非常大。尝试减小它们的大小,看看它是否再次发生。如果可以的话,也可以尝试避免使用过多的透支。