【发布时间】:2018-03-03 07:23:40
【问题描述】:
我在我的应用程序中使用 wuapnjie/StickerView library,当我在 onCreate 方法中调用该方法时,StickerView 类中的函数 getCurrentSticker 之一返回空对象引用,但从任何 onClick 方法调用时都能正常工作。您的任何专业知识都会非常有帮助。谢谢。
当我尝试在 runOnUiThread 中的单独线程中运行代码时,有时它可以工作,有时不能。为什么有些代码会在某个时候起作用,而不是其他时候。我很迷茫。下面是在 onClick 方法中工作而不在 onCreate 方法中工作的代码。
当我在下面的代码中从 onCreate 调用 addStickerNow 时,它返回空值。在 onClick 方法中,它返回的是实际值。
public class MainActiviy extends AppCompatActivity
{
private StickerView stickerView;
private Button button;
LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.lnr_draw);
stickerView = (StickerView) findViewById(R.id.sticker_view);
button = (Button) findViewById(R.id.button3);
addStickerNow(); //It's not working.
}
public void onButtonClicked(View view)
{
addStickerNow(); //It's working.
}
public void addStickerNow()
{
stickerView.addSticker(
new TextSticker(getApplicationContext())
.setText("TEXT")
.setMaxTextSize(70)
.setTextAlign(Layout.Alignment.ALIGN_CENTER)
.setMinTextSize(100)
, Sticker.Position.TOP);
int test = 1; //1st breakpoint
Sticker sticker = stickerView.getCurrentSticker(); // 2nd breakpoint
int test2 = 1; //3rd breakpoint
}
}
我尝试按照 Mr.Hyde 的建议调试应用,发现从 onCreate 和 onClick 调用 addStickerNow 时出现以下差异。
*1st breakpoint, when called from onCreate
this = {MainActivity@4424}
stickerView = {StickerView@4427} "com.xiaopo.flying.sticker.StickerView{7b55d6d V.E...... ......I. 0,0-0,0 #7f070071 app:id/sticker_view}"
1st breakpoint, when called from onClick
this = {MainActivity@4424}
stickerView = {StickerView@4427} "com.xiaopo.flying.sticker.StickerView{7b55d6d V.E...... ......ID 0,160-720,760 #7f070071 app:id/sticker_view}"*
这里的主要区别是 StickerView 的 RectF 在从 onCreate 调用时是 0,0-0,0 而从 onClick 调用时是 0,160-720,760。所以它与布局创建有关。
布局文件如下。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/parent_layout"
tools:context="e.venkihero.stickerviewtest.MainActivity">
<LinearLayout
android:id="@+id/lnr_draw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:orientation="vertical">
<com.xiaopo.flying.sticker.StickerView
android:id="@+id/sticker_view"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_gravity="center"
app:showIcons="true">
<ImageView
android:id="@+id/img_bg1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:scaleType="fitXY" />
</com.xiaopo.flying.sticker.StickerView>
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="100dp"
android:onClick="onButtonClicked"
android:clickable="true" />
</LinearLayout>
如果有人能解释为什么这段代码(位于顶部)在 onClick 方法中运行良好,但在 onCreate 方法中返回空对象引用,那将非常有帮助。谢谢。
【问题讨论】:
-
请添加您的
MainActivity的一部分。特别是OnCreate. -
添加代码你是如何从 MainActivity OnCreate 方法调用它的?
-
@Mr.Hyde 我最初添加的第一段代码仅来自 onCreate。现在我对其进行了编辑以查看它是从 onCreate 调用的。 (注意:这不是我实际的 MainActivity,但我正在尝试这样做。当我从 onCreate 调用 addStickerNow 时,返回 Null 指针,但是当它从 onClick 方法(onButtonClicked)调用时,它返回实际值。
-
@MilanHirpara 请立即查看更新后的代码
-
@Mr.Hyde Nitinkumar 的以下回答有效。这一切都来自您调试应用程序的建议。非常感谢。
标签: android onclick nullreferenceexception oncreate