【发布时间】:2014-07-10 16:57:27
【问题描述】:
我从一个具有surfaceView 的类中创建了一个“runGraphics.java”类的新实例。当它打开这个类时,它现在在放入 setContentView 时遇到问题。在调用 runGraphics 类之前在不同的函数中调用它之前。 这是我的活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class runGraphics extends Activity
{
ImageButton polarCap1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.graphics);
polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
polarCap1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(runGraphics.this, "IT WORKED", Toast.LENGTH_LONG).show();
}//end onClick function
});//end setOnClickListener
}//end onCreate function
}//end runGraphics class
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/polarCapButton"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/polarcap" />
</LinearLayout>
这是我添加 setContentView 后的 logcat:
05-21 16:52:38.726: E/AndroidRuntime(18959): FATAL EXCEPTION: Thread-17
05-21 16:52:38.726: E/AndroidRuntime(18959): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-21 16:52:38.726: E/AndroidRuntime(18959): at android.os.Handler.<init>(Handler.java:121)
05-21 16:52:38.726: E/AndroidRuntime(18959): at android.app.Activity.<init>(Activity.java:704)
05-21 16:52:38.726: E/AndroidRuntime(18959): at com.twentytwentythree.sab.runGraphics.<init>(runGraphics.java:11)
05-21 16:52:38.726: E/AndroidRuntime(18959): at com.twentytwentythree.sab.GraphicsSurface$SetupGraphicsSurface.run(GraphicsSurface.java:280)
05-21 16:52:38.726: E/AndroidRuntime(18959): at java.lang.Thread.run(Thread.java:1019)
【问题讨论】:
-
此设置不允许我单击图像按钮。如果我触摸它,它什么也不做。
-
你有线程或异步任务吗?
-
在调用我的 runGraphics 类的类中,我让它在 onResume 函数中创建一个名为“ourthread”的新线程。同样在 onResume 中,一个名为 isRunning 的布尔变量设置为 true,因此 run 函数将在它为 true 时运行。还有一个扩展 Async 的不同类,但它是独立的并保存单独的信息。
-
你的错误是线程,因为你正在尝试向 UI 发送一些消息,放 Looper.prepare()。
-
在你的线程中,在向 UI 发送数据或一些信息之前,放入 Looper.prepare()
标签: java android image button imagebutton