1

我正在尝试在onItemSelected. 线路有问题:

selectLayout.addView(v);

当我删除此代码时,它正在运行。

日志猫:

09-30 08:53:48.493: I/Choreographer(1033): Skipped 63 frames!  The application may be doing too much work on its main thread.
09-30 08:53:48.843: D/gralloc_goldfish(1033): Emulator without GPU emulation detected.
09-30 08:53:48.963: I/Choreographer(1033): Skipped 46 frames!  The application may be doing too much work on its main thread.
09-30 08:54:19.894: D/dalvikvm(1033): GC_FOR_ALLOC freed 82K, 7% free 2893K/3096K, paused 135ms, total 170ms
09-30 08:54:19.935: I/dalvikvm-heap(1033): Grow heap (frag case) to 3.543MB for 635812-byte allocation
09-30 08:54:20.124: D/dalvikvm(1033): GC_FOR_ALLOC freed 9K, 6% free 3505K/3720K, paused 183ms, total 183ms
09-30 08:54:20.864: I/Choreographer(1033): Skipped 129 frames!  The application may be doing too much work on its main thread.
09-30 08:54:23.363: I/Choreographer(1033): Skipped 91 frames!  The application may be doing too much work on its main thread.
09-30 08:54:24.215: D/AndroidRuntime(1033): Shutting down VM
09-30 08:54:24.215: W/dalvikvm(1033): threadid=1: thread exiting with uncaught exception (group=0x41465700)
09-30 08:54:24.264: E/AndroidRuntime(1033): FATAL EXCEPTION: main
09-30 08:54:24.264: E/AndroidRuntime(1033): java.lang.NullPointerException
09-30 08:54:24.264: E/AndroidRuntime(1033):     at com.example.fitapp.Select$1.onItemSelected(Select.java:67)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.widget.AdapterView.access$200(AdapterView.java:49)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.os.Handler.handleCallback(Handler.java:730)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.os.Looper.loop(Looper.java:137)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invoke(Method.java:525)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 08:54:24.264: E/AndroidRuntime(1033):     at dalvik.system.NativeStart.main(Native Method)
09-30 08:54:28.833: I/Process(1033): Sending signal. PID: 1033 SIG: 9
09-30 08:54:32.354: I/Choreographer(1054): Skipped 36 frames!  The application may be doing too much work on its main thread.
09-30 08:54:33.263: I/Choreographer(1054): Skipped 36 frames!  The application may be doing too much work on its main thread.
09-30 08:54:33.363: D/gralloc_goldfish(1054): Emulator without GPU emulation detected.

这也是我所有的代码。

package com.example.fitapp;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class Select extends Activity{

    TextView tvSelect, t;
    Spinner sSelect;
    LinearLayout selectLayout;
    String[] values;
    String data;
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);

        selectLayout = (LinearLayout) findViewById(R.id.saffet);
        LayoutInflater inf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View v = inf.inflate(R.layout.second, null);

        final Map<String,String> map = new HashMap<String,String>();
        map.put("1", "Kuvvet kazanmak istiyorsun demek?");
        map.put("2", "Kas yapmak istiyorsun demek?");
        map.put("3", "O lanet olasi yaglari eritelim!");

        Resources r = getResources();
        values = r.getStringArray(R.array.values);

        sSelect = (Spinner) findViewById(R.id.sSelect);
        tvSelect = (TextView) findViewById(R.id.tvSelect);

        t = new TextView(Select.this);

        sSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> av, View view,
                    int position, long arg3) {
                // TODO Auto-generated method stub

                if(count!=0)
                {
                    data = values[position];
                    Dialog d = new Dialog(Select.this);
                    d.setTitle("Tamam!");
                    t.setText(map.get(data));
                    t.setPadding(5, 5, 5, 5);
                    d.setContentView(t);
                    d.show();

                    selectLayout.addView(v);
                }

                count = count + 1;

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

}

选择.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/selectLayout"
    >

    <TextView
        android:id="@+id/tvSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Amaç Seçiniz!"
        android:textSize="18sp"
        android:layout_gravity="center"
         />

    <Spinner
        android:id="@+id/sSelect"
        android:layout_width="179dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:entries="@array/amaclar"
        android:prompt="@string/select"
         />

</LinearLayout>
4

1 回答 1

1

您的 LinearLayout id 是

 android:id="@+id/selectLayout"

但是你有这个

selectLayout = (LinearLayout) findViewById(R.id.saffet);

所以改为

selectLayout = (LinearLayout) findViewById(R.id.selectLayout);

应用程序可能在其主线程上做了太多工作。

您的主线程是 ui 线程。作为 commonsware 建议使用 traceview。您是否还有其他活动会进行一些密集计算,因为我在Select.java. 如果是这样,请使用threador asynctask

于 2013-09-30T13:11:54.053 回答