我的自定义视图出错:

时间:2013-10-12 13:04:13

标签: java android android-custom-view

您好我正在开发应用程序,拍摄所有照片并评价现在我的CustomView有问题(idont知道如何使用自定义视图)这就是我想要做的: a busy cat

现在这是我已经粉碎的代码在调用自定义视图后函数:

MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
    layout = new Layout();
    new Events(layout);
}
Layout layout;
public class Layout{
    public Layout()
    {
        btnStartMeinu = (Button)findViewById(R.id.btnStartMeinu);
    }
    Button btnStartMeinu;
}
public class Events{
    public Events(Layout layout){
        layout.btnStartMeinu.setOnClickListener(new btnStart_OnClick());

    }
    class btnStart_OnClick implements OnClickListener {

        @Override
        public void onClick(View view) {
            btn_OnClick(view);

        }
    }
}
void btn_OnClick(View view){
    Intent intent = new Intent(this, MainMenuActivity.class);
            startActivity(intent);
}
}

MainMenuActivity.java:   addCostumView(); - 调用自定义视图

public class MainMenuActivity extends Activity {
Layout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maine_menu);
    layout = new Layout();
    new Events(layout);
       addCostumView();
}
public class Layout{
    public Layout(){
        linearLayout = (LinearLayout)findViewById(R.id.linearlayout1);
    }
    LinearLayout linearLayout;     
}
public class Events{
    public Events(Layout layout){

    }
}
public void addCostumView(){
    // LinearLayout layout = new LinearLayout(this);
    for(int i = 1; i<10 ; i++)
    {
        CustomView custom = new CustomView(null, null);
        // changes to yours custom view
        layout.linearLayout.addView(custom);
    }
}
}

CustomView.java:

public class CustomView extends LinearLayout{
public CustomView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.costum_view_photos, this);
    Button btn1 = (Button)findViewById(R.id.btn1);


}

}

logcat的:

10-12 13:13:53.531      277-490/system_process I/ActivityManager: START u0 {cmp=com.example.photosnetwork/.MainMenuActivity} from pid 838
10-12 13:13:53.541      277-490/system_process W/WindowManager: Failure taking screenshot for (246x410) to layer 21015
10-12 13:13:53.751      838-838/com.example.photosnetwork D/dalvikvm: GC_FOR_ALLOC freed 52K, 7% free 2658K/2832K, paused 28ms, total 38ms
10-12 13:13:53.761      838-838/com.example.photosnetwork I/dalvikvm-heap: Grow heap (frag case) to 3.318MB for 635812-byte allocation
10-12 13:13:53.861      838-847/com.example.photosnetwork D/dalvikvm: GC_FOR_ALLOC freed 6K, 6% free 3273K/3456K, paused 101ms, total 101ms
10-12 13:13:53.992      838-840/com.example.photosnetwork D/dalvikvm: GC_CONCURRENT freed <1K, 6% free 3278K/3456K, paused 3ms+89ms, total 131ms
10-12 13:13:54.001      838-838/com.example.photosnetwork D/AndroidRuntime: Shutting down VM
10-12 13:13:54.001      838-838/com.example.photosnetwork W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-12 13:13:54.031      838-838/com.example.photosnetwork E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.photosnetwork/com.example.photosnetwork.MainMenuActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
        at android.view.View.<init>(View.java:3236)
        at android.view.View.<init>(View.java:3281)
        at android.view.ViewGroup.<init>(ViewGroup.java:431)
        at android.widget.LinearLayout.<init>(LinearLayout.java:176)
        at android.widget.LinearLayout.<init>(LinearLayout.java:172)
        at com.example.photosnetwork.CustomView.<init>(CustomView.java:13)
        at com.example.photosnetwork.MainMenuActivity.addCostumView(MainMenuActivity.java:37)
        at com.example.photosnetwork.MainMenuActivity.onCreate(MainMenuActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        ... 11 more
10-12 13:13:54.051      277-378/system_process D/SystemClock: Setting time of day to sec=1381583644
10-12 13:14:04.222      277-378/system_process W/SystemClock: Unable to set rtc to 1381583644: Invalid argument
10-12 13:14:04.367      277-289/system_process W/ActivityManager: Force finishing activity com.example.photosnetwork/.MainMenuActivity
10-12 13:14:04.367      277-289/system_process W/ActivityManager: Force finishing activity com.example.photosnetwork/.MainActivity
10-12 13:14:04.507      277-294/system_process D/dalvikvm: GC_FOR_ALLOC freed 287K, 19% free 6323K/7720K, paused 59ms, total 64ms
10-12 13:14:04.507      277-294/system_process I/dalvikvm-heap: Grow heap (frag case) to 6.902MB for 635812-byte allocation
10-12 13:14:04.587      277-855/system_process D/dalvikvm: GC_FOR_ALLOC freed 12K, 17% free 6931K/8344K, paused 69ms, total 69ms
10-12 13:14:04.867        37-85/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
10-12 13:14:04.877      277-291/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{410d9980 u0 com.example.photosnetwork/.MainMenuActivity}
10-12 13:14:05.367      594-609/com.android.deskclock V/AlarmClock: No next alarm
10-12 13:14:05.377      594-609/com.android.deskclock V/AlarmClock: AlarmInitReceiver finished
10-12 13:14:05.507      594-594/com.android.deskclock I/Choreographer: Skipped 112 frames!  The application may be doing too much work on its main thread.
10-12 13:14:05.587       37-312/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
10-12 13:14:10.897      619-637/com.android.exchange D/ExchangeService: Received deviceId from Email app: null
10-12 13:14:10.897      619-637/com.android.exchange D/ExchangeService: !!! deviceId unknown; stopping self and retrying
10-12 13:14:11.218      277-277/system_process I/AudioService: AudioFocus  abandonAudioFocus() from android.media.AudioManager@40ce9818com.android.music.MediaPlaybackService$3@40ce8438
10-12 13:14:15.829      277-291/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{41042a30 u0 com.example.photosnetwork/.MainActivity}
10-12 13:14:15.829      277-291/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{410d9980 u0 com.example.photosnetwork/.MainMenuActivity}
10-12 13:14:15.926      619-619/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
10-12 13:14:15.936      277-414/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
10-12 13:14:15.936      619-636/com.android.exchange D/ExchangeService: !!! Email application not found; stopping self
10-12 13:14:15.946      277-288/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
10-12 13:14:15.967      619-619/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04ec8 that was originally bound here
        android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04ec8 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
        at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
        at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
10-12 13:14:15.976      619-619/com.android.exchange E/StrictMode: null
        android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04ec8 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
        at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
        at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
10-12 13:14:15.976      277-277/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@4103efd8
10-12 13:14:15.987      619-619/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04b20 that was originally bound here
        android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04b20 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
        at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
10-12 13:14:15.996      619-619/com.android.exchange E/StrictMode: null
        android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d04b20 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
        at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
10-12 13:14:15.996      277-288/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@41073ad8

1 个答案:

答案 0 :(得分:0)

当您从costum_view_photos布局中充气的视图访问btn1时,您的CustomView类中出现NullPointerException

尝试这样: -

public class CustomView extends LinearLayout{
public CustomView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view= inflater.inflate(R.layout.costum_view_photos, this);
    Button btn1 = (Button) view.findViewById(R.id.btn1);//changed here

}
}

希望这会有所帮助.....