我遇到了btn.setText()
的问题,但可以setOnClickListener()
logcat告诉我:
java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence)'在com.gc.materialdesign.views.Button.setText(Button.java:185)上的空对象引用上
我尝试修复但失败了, 这是.java代码:
package com.example.rain.fishingcard;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
com.gc.materialdesign.views.ButtonRectangle startbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbtn = (com.gc.materialdesign.views.ButtonRectangle) findViewById(R.id.startbtn);
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(MainActivity.this, GameActivity.class);
//startActivity(intent);
startbtn.setText("I like");
}
});
}
}
,它是.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.rain.fishingcard.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fishing Card!"
android:layout_marginTop="59dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/startText" />
<com.gc.materialdesign.views.ButtonRectangle
android:id="@+id/startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
android:text="start"
android:layout_marginTop="48dp"
android:layout_below="@+id/startText"
android:layout_alignLeft="@+id/startText"
android:layout_alignStart="@+id/startText" />
<com.gc.materialdesign.views.ButtonRectangle
android:id="@+id/settingbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E88E5"
android:text="setting"
android:visibility="gone"
android:layout_below="@+id/startbtn"
android:layout_alignLeft="@+id/startbtn"
android:layout_alignStart="@+id/startbtn" />
</RelativeLayout>
这是logcat:
06-19 00:26:23.627 2130-2130/com.example.rain.fishingcard E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.example.rain.fishingcard, PID: 2130
Theme: themes:{}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.gc.materialdesign.views.Button.setText(Button.java:185)
at com.example.rain.fishingcard.MainActivity$1.onClick(MainActivity.java:24)
at com.gc.materialdesign.views.Button.makeCircle(Button.java:135)
at com.gc.materialdesign.views.ButtonRectangle.onDraw(ButtonRectangle.java:145)
at android.view.View.draw(View.java:16187)
at android.view.View.updateDisplayListIfDirty(View.java:15184)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15144)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2623)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2442)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2075)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-19 00:31:23.342 2130-2130/com.example.rain.fishingcard I/Process: Sending signal. PID: 2130 SIG: 9
答案 0 :(得分:0)
请更改您的代码:
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(MainActivity.this, GameActivity.class);
//startActivity(intent);
startbtn.setText("I like");
}
});
代码:
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent intent = new Intent(MainActivity.this, GameActivity.class);
//startActivity(intent);
TextView textButton = new TextView(getContext());
textButton.setText("I like");
startbtn.setText(textButton);
}
});
这将解决您的问题。
答案 1 :(得分:0)
尝试下载库并将其作为模块导入到您的应用中,然后尝试用这两个类ButtonRectangle和ButtonFlat替换两个类ButtonRectangle.java和ButtonFlat.java然后你很好去吧。