添加视图不是出现在屏幕上的布局以外的布局 [英] adding view to a layout other than the layout appearing on screen
问题描述
我有一个按键主要布局,当按钮被触动就会切换使用的setContentView
布局和按钮添加到新观察布局。
我得到一个运行时错误。
我的code:
包com.android.ui;进口android.app.Activity;进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.widget.Button;
进口android.widget.FrameLayout;
进口android.widget.FrameLayout.LayoutParams;
进口android.widget.ImageView;公共类mainprog扩展活动实现OnTouchListener {
/ **当第一次创建活动调用。 * /
私人最终静态INT START_DRAGGING = 0;
私人最终静态INT STOP_DRAGGING = 1;
公共按钮BTN1;
公共按钮BTN;
公众的FrameLayout布局;
私人诠释的地位;
公众的LayoutParams PARAMS;
公众的FrameLayout布局1;
公众形象ImageView的;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 布局=(的FrameLayout)findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(本);
布局1 =(的FrameLayout)findViewById(R.id.LinearLayout02);
BTN1 =(按钮)findViewById(R.id.btn1);
BTN =(按钮)findViewById(R.id.btn);
btn.setDrawingCacheEnabled(真);
btn.setOnTouchListener(本);
PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT); } 公共布尔onTouch(视图V,MotionEvent我){ 如果(me.getAction()== MotionEvent.ACTION_DOWN){
状态= START_DRAGGING;
图像=新ImageView的(本);
image.setImageBitmap(btn.getDrawingCache());
的setContentView(R.layout.draw);
layout1.addView(图像,则params); } 返回false; }}
我的main.xml
<?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:比重=中心机器人:ID =@ + ID / LinearLayout01
机器人:paddingLeft =0dip><按钮的android:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / BTN。
机器人:文字=拖动我
>< /按钮>
< /&的FrameLayout GT;`
我draw.xml
<?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:比重=中心
机器人:ID =@ + ID / LinearLayout02
机器人:paddingLeft =0dip
><按钮
机器人:layout_width =WRAP_CONTENT
机器人:ID =@ + ID / BTN1
机器人:layout_height =WRAP_CONTENT
机器人:文字=拖放Meeeeeee
>< /按钮>
< /&的FrameLayout GT;`
logcat的
`I / ActivityManager(52):显示活动com.android.ui / .mainprog:1156毫秒(共1156毫秒) D / AndroidRuntime(260):关闭VM W / dalvikvm(260):主题ID = 3:螺纹未捕获的异常退出(组= 0x4001b188) E / AndroidRuntime(260):未捕获的处理程序:螺纹主力退出,由于未捕获的异常 E / AndroidRuntime(260):显示java.lang.NullPointerException E / AndroidRuntime(260):在com.android.ui.mainprog.onTouch(mainprog.java:47) E / AndroidRuntime(260):在android.view.View.dispatchTouchEvent(View.java:3705) E / AndroidRuntime(260):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822) E / AndroidRuntime(260):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822) E / AndroidRuntime(260):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822) E / AndroidRuntime(260):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822) E / AndroidRuntime(260):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) E / AndroidRuntime(260):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) E / AndroidRuntime(260):在android.app.Activity.dispatchTouchEvent(Activity.java:2061) E / AndroidRuntime(260):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643) E / AndroidRuntime(260):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691) E / AndroidRuntime(260):在android.os.Handler.dispatchMessage(Handler.java:99) E / AndroidRuntime(260):在android.os.Looper.loop(Looper.java:123) E / AndroidRuntime(260):在android.app.ActivityThread.main(ActivityThread.java:4363) E / AndroidRuntime(260):在java.lang.reflect.Method.invokeNative(本机方法) E / AndroidRuntime(260):在java.lang.reflect.Method.invoke(Method.java:521) E / AndroidRuntime(260):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) E / AndroidRuntime(260):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) E / AndroidRuntime(260):在dalvik.system.NativeStart.main(本机方法) I /过程(52):发送信号。 PID:260 SIG:3 I / dalvikvm(260):主题ID = 7:反应以信号3 I / dalvikvm(260):写堆栈跟踪/data/anr/traces.txt I /处理(260):发送信号。 PID:260 SIG:9 I / ActivityManager(52):过程com.android.ui(PID 260)已经死亡。 I /窗口管理器(52):WIN死亡:窗口{43d379c0 com.android.ui / com.android.ui.mainprog暂停= FALSE} I / usagestats机(52):com.android.launcher意外的简历,同时已在com.android.ui恢复 E / gralloc(52):[注销]处理0x4c23d0仍处于锁定状态(状态= 40000001) W / InputManagerService(52):GOT的RemoteException发送SETACTIVE(假)的通知为PID 260 UID 10024`
您的方法来解决这种情况是错误的犯错
的setContentView(R.layout.main);
布局=(的FrameLayout)findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(本);
布局1 =(的FrameLayout)犯错
所以在这里你对main.xml中设置,这就是为什么布局1将是空的。
我会建议你使用按钮onClickListener,到位ontouch的
然后用充气布局加载XML布局视图对象onClick的方法和内部
然后使用该视图对象调用findviewById像 obj.findViewById(R.id.LinearLayout01);
然后添加图像,它会解决你的问题。
I have the main layout with a button, when the button is touched it will switch the layout using setcontentView
and add a button to the newly viewed layout.
I get a runtime error.
my code:
package com.android.ui;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
public class mainprog extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
public Button btn1;
public Button btn;
public FrameLayout layout;
private int status;
public LayoutParams params;
public FrameLayout layout1;
public ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
layout1 = (FrameLayout) findViewById(R.id.LinearLayout02);
btn1 = (Button) findViewById(R.id.btn1);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
setContentView(R.layout.draw);
layout1.addView(image,params);
}
return false;
}
}
my main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:id="@+id/LinearLayout01"
android:paddingLeft="0dip">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btn"
android:text="Drag Me"
></Button>
</FrameLayout>`
my draw.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="@+id/LinearLayout02"
android:paddingLeft="0dip"
>
<Button
android:layout_width="wrap_content"
android:id="@+id/btn1"
android:layout_height="wrap_content"
android:text="Drag Meeeeeee"
></Button>
</FrameLayout>`
LOGCAT
`I/ActivityManager( 52): Displayed activity com.android.ui/.mainprog: 1156 ms (total 1156 ms)
D/AndroidRuntime( 260): Shutting down VM
W/dalvikvm( 260): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 260): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 260): java.lang.NullPointerException
E/AndroidRuntime( 260): at com.android.ui.mainprog.onTouch(mainprog.java:47)
E/AndroidRuntime( 260): at android.view.View.dispatchTouchEvent(View.java:3705)
E/AndroidRuntime( 260): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
E/AndroidRuntime( 260): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
E/AndroidRuntime( 260): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
E/AndroidRuntime( 260): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
E/AndroidRuntime( 260): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
E/AndroidRuntime( 260): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime( 260): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
E/AndroidRuntime( 260): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
E/AndroidRuntime( 260): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
E/AndroidRuntime( 260): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 260): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 260): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 260): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 260): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 260): at dalvik.system.NativeStart.main(Native Method)
I/Process ( 52): Sending signal. PID: 260 SIG: 3
I/dalvikvm( 260): threadid=7: reacting to signal 3
I/dalvikvm( 260): Wrote stack trace to '/data/anr/traces.txt'
I/Process ( 260): Sending signal. PID: 260 SIG: 9
I/ActivityManager( 52): Process com.android.ui (pid 260) has died.
I/WindowManager( 52): WIN DEATH: Window{43d379c0 com.android.ui/com.android.ui.mainprog paused=false}
I/UsageStats( 52): Unexpected resume of com.android.launcher while already resumed in com.android.ui
E/gralloc ( 52): [unregister] handle 0x4c23d0 still locked (state=40000001)
W/InputManagerService( 52): Got RemoteException sending setActive(false) notification to pid 260 uid 10024 `
Your approach to solve this situation is wrong mistake first
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
layout1 = (FrameLayout) mistake first
so here your on main.xml is set that's why layout1 will be null.
I will advise you to use onClickListener of button, in place of ontouch
then inside the onClick method using layout inflator load XML layout to a view object and
then using that view object call findviewById like obj.findViewById(R.id.LinearLayout01);
then add image in that, it will solve your problem .
这篇关于添加视图不是出现在屏幕上的布局以外的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!