尝试通过单击按钮更改 textView 中的文本大小时应用程序崩溃
App Crash When Trying to Change textsize in textView by clicking on button
在我的一个 Activity 中,我有一个带有文本选项的菜单选项,当用户单击它时,它会重定向到另一个布局,其中我保留了两个按钮,一个是 加号按钮 另一个是 减号按钮
当用户单击该按钮时我想更改 BackStack 的 TextView 文本大小Activity
下面是我的代码 当我点击 MenuOPtion 时,应用程序崩溃了,它在 Logcat
中报告 NullPointerException
我的详细信息Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
detailtext= (TextView) findViewById(R.id.detail);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);
if(cursor.moveToFirst())
{
detailtext.setText(cursor.getString(0));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id==R.id.text_option)
{
Intent intent= new Intent(this,CustomSetting.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Detail_menu.xml
<item android:id="@+id/text_option"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="Text Option"
/>
CustomSetting.java
public class CustomSetting extends AppCompatActivity {
DetailActivity detailActivity;
ImageButton plus,minus,close;
int txtSize = 14;
TextView detail=detailActivity.detailtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_setting);
plus= (ImageButton) findViewById(R.id.plus);
minus= (ImageButton) findViewById(R.id.minus);
close= (ImageButton) findViewById(R.id.close);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (detail.getTextSize() == 14 ||detail.getTextSize() == 15 || detail.getTextSize() == 16 || detail.getTextSize() == 17){
txtSize++;
detail.setTextSize(txtSize);
} else if (detail.getTextSize() == 18) {
txtSize+= 0;
detail.setTextSize(txtSize);
} else{
txtSize+= 0;
detail.setTextSize(txtSize);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (detail.getTextSize() == 15||detail.getTextSize() ==16||detail.getTextSize() ==17|detail.getTextSize() ==18){
txtSize--;
detail.setTextSize(txtSize);
} else {
txtSize += 0;
detail.setTextSize(txtSize);
}
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
startActivity(intent);
}
});
}
custom_setting.xml
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/close"
android:layout_above="@+id/textView"
android:layout_toRightOf="@+id/minus"
android:layout_toEndOf="@+id/minus"
android:id="@+id/close"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/plusign"
android:layout_marginTop="110dp"
android:id="@+id/plus"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/minusign"
android:id="@+id/minus"
android:layout_alignTop="@+id/plus"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Change Text Size"
android:id="@+id/textView"
android:layout_above="@+id/minus"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
/>
Logcat:
Process: com.example.aa.sidd, PID: 23413
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa.sidd/com.example.aa.sidd.CustomSetting}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.access0(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
if (id==R.id.text_option)
但是菜单的 id 显示正确,如下所述
item android:id="@+id/display"
所以我觉得应该是
if (id==R.id.display) //like this
我假设您在 DetailActivity 中有一些行,例如
Textview detailtext;
改为
static Textview detailtext;
然后摆脱你的
DetailActivity detailActivity;
并更改行
TextView detail=detailActivity.detailtext;
到
TextView detail=DetailActivity.detailtext;
希望这对您有所帮助。
虽然这个问题已经被接受了答案,但是使用静态控件引用是不好的编程习惯。
相反,您有数据传递的意图,因此在打开 CustomSetting activity 时,您应该将 textview textsize 作为 intent 额外传递,如下所示:-
Intent intent= new Intent(this,CustomSetting.class);
intent.putExtra("textview_size",detailtext.getTextSize());
startActivity(intent);
在 CustomSetting onCreate 中你可以做
if (getIntent().hasExtra("textview_size"){
font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
}
点击 + 或 - 按钮时,您可以使用
font_size += 1
或
fontsize -= 1
当点击关闭按钮时你可以做
Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
intent.putExtra("textview_size",fontsize);
startActivity(intent);
并且在你的 DetailActivity oncreate 中你可以做
if (getIntent().hasExtra("textview_size"){
font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
detailtext.setTextSize(font_size);
}
三件事,
来自您的原始问题:
你会因为试图访问一个未初始化的对象而得到一个空指针异常。在这种情况下 activity.
DetailActivity detailActivity;
ImageButton plus,minus,close;
/.../
TextView detail=detailActivity.detailtext;
并且您尝试执行此操作的方式不可能实现您的尝试。请从一位著名的 android 专家那里看到这个 link。
which answers a similar question here Get reference of one Activity from another in android
另一个可能的问题是您的资源。您正在加载多个图像按钮,确保您的图像大小(字节)不会太大。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa.sidd/com.example.aa.sidd.CustomSetting}: android.view.InflateException: Binary XML file line #17: Error inflating class
看到这个问题:
android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
第三,正如我在评论中提到的,我不认为将成员声明为静态作为解决方法是个好主意,请参阅此处 Avoiding memory leaks 静态成员、方法和 类 需要在计划任何应用程序时谨慎使用并考虑。用任何语言。嗯,这就是我的看法。
以DialogFragment 的形式显示新布局。这个想法是以 DialogFragment 弹出窗口的形式显示 increase/Decrease 选项。您可以从弹出窗口中 increase/decrease 大小。弹出窗口关闭后,所选大小将设置为 Activity 中的 TextView。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static int updatedSize;
TextView greetingText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
greetingText = (TextView)findViewById(R.id.hello_world_text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
mycustomLayout();
return true;
}
return super.onOptionsItemSelected(item);
}
public void mycustomLayout(){
FragmentManager manager = getFragmentManager();
PopUp popUp = new PopUp(new FragmentDismissHandler());
popUp.show(manager,"POPUP");
}
public void refreshText(){
if( updatedSize>0)
greetingText.setTextSize(updatedSize);
}
private class FragmentDismissHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
refreshText();
}
}
}
PopUp.java(自定义布局为 DialogFragment)
public class PopUp extends DialogFragment implements View.OnClickListener{
ImageButton increase,decrease,set;
TextView sizeOfText;
static int size;
Handler handler;
public PopUp(Handler handler) {
this.handler = handler;
}
public PopUp(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
getDialog().setTitle("change text size");
View view = inflater.inflate(R.layout.my_custom_layout, container, false);
increase = (ImageButton)view.findViewById(R.id.inc);
increase.setOnClickListener(this);
decrease = (ImageButton)view.findViewById(R.id.dec);
decrease.setOnClickListener(this);
sizeOfText = (TextView)view.findViewById(R.id.size_text);
sizeOfText.setText("text");
set = (ImageButton)view.findViewById(R.id.ok);
set.setOnClickListener(this);
size = (int) sizeOfText.getTextSize();
return view;
}
@Override
public void onClick(View view) {
int id= view.getId();
if(id==R.id.inc){
size = size +1;
sizeOfText.setText("text");
sizeOfText.setTextSize(size);
}
if(id== R.id.dec){
size = size - 1;
sizeOfText.setText("text");
sizeOfText.setTextSize(size);
}
if(id== R.id.ok){
dismiss();
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
MainActivity.updatedSize = size;
handler.sendEmptyMessage(0);
}
}
在我的一个 Activity 中,我有一个带有文本选项的菜单选项,当用户单击它时,它会重定向到另一个布局,其中我保留了两个按钮,一个是 加号按钮 另一个是 减号按钮
当用户单击该按钮时我想更改 BackStack 的 TextView 文本大小Activity
下面是我的代码 当我点击 MenuOPtion 时,应用程序崩溃了,它在 Logcat
中报告 NullPointerException我的详细信息Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
detailtext= (TextView) findViewById(R.id.detail);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);
if(cursor.moveToFirst())
{
detailtext.setText(cursor.getString(0));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id==R.id.text_option)
{
Intent intent= new Intent(this,CustomSetting.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
Detail_menu.xml
<item android:id="@+id/text_option"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="Text Option"
/>
CustomSetting.java
public class CustomSetting extends AppCompatActivity {
DetailActivity detailActivity;
ImageButton plus,minus,close;
int txtSize = 14;
TextView detail=detailActivity.detailtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_setting);
plus= (ImageButton) findViewById(R.id.plus);
minus= (ImageButton) findViewById(R.id.minus);
close= (ImageButton) findViewById(R.id.close);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (detail.getTextSize() == 14 ||detail.getTextSize() == 15 || detail.getTextSize() == 16 || detail.getTextSize() == 17){
txtSize++;
detail.setTextSize(txtSize);
} else if (detail.getTextSize() == 18) {
txtSize+= 0;
detail.setTextSize(txtSize);
} else{
txtSize+= 0;
detail.setTextSize(txtSize);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (detail.getTextSize() == 15||detail.getTextSize() ==16||detail.getTextSize() ==17|detail.getTextSize() ==18){
txtSize--;
detail.setTextSize(txtSize);
} else {
txtSize += 0;
detail.setTextSize(txtSize);
}
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
startActivity(intent);
}
});
}
custom_setting.xml
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/close"
android:layout_above="@+id/textView"
android:layout_toRightOf="@+id/minus"
android:layout_toEndOf="@+id/minus"
android:id="@+id/close"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/plusign"
android:layout_marginTop="110dp"
android:id="@+id/plus"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<ImageButton
android:layout_width="50dp"
android:layout_height="40dp"
android:background="@drawable/minusign"
android:id="@+id/minus"
android:layout_alignTop="@+id/plus"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Change Text Size"
android:id="@+id/textView"
android:layout_above="@+id/minus"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
/>
Logcat:
Process: com.example.aa.sidd, PID: 23413
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa.sidd/com.example.aa.sidd.CustomSetting}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.access0(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
if (id==R.id.text_option)
但是菜单的 id 显示正确,如下所述
item android:id="@+id/display"
所以我觉得应该是
if (id==R.id.display) //like this
我假设您在 DetailActivity 中有一些行,例如
Textview detailtext;
改为
static Textview detailtext;
然后摆脱你的
DetailActivity detailActivity;
并更改行
TextView detail=detailActivity.detailtext;
到
TextView detail=DetailActivity.detailtext;
希望这对您有所帮助。
虽然这个问题已经被接受了答案,但是使用静态控件引用是不好的编程习惯。
相反,您有数据传递的意图,因此在打开 CustomSetting activity 时,您应该将 textview textsize 作为 intent 额外传递,如下所示:-
Intent intent= new Intent(this,CustomSetting.class);
intent.putExtra("textview_size",detailtext.getTextSize());
startActivity(intent);
在 CustomSetting onCreate 中你可以做
if (getIntent().hasExtra("textview_size"){
font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
}
点击 + 或 - 按钮时,您可以使用
font_size += 1
或
fontsize -= 1
当点击关闭按钮时你可以做
Intent intent= new Intent(CustomSetting.this,DetailActivity.class);
intent.putExtra("textview_size",fontsize);
startActivity(intent);
并且在你的 DetailActivity oncreate 中你可以做
if (getIntent().hasExtra("textview_size"){
font_size = getIntent().getFloatExtra("textview_size",<default_size_your_want>);
detailtext.setTextSize(font_size);
}
三件事,
来自您的原始问题:
你会因为试图访问一个未初始化的对象而得到一个空指针异常。在这种情况下 activity.
DetailActivity detailActivity;
ImageButton plus,minus,close;
/.../
TextView detail=detailActivity.detailtext;
并且您尝试执行此操作的方式不可能实现您的尝试。请从一位著名的 android 专家那里看到这个 link。
which answers a similar question here Get reference of one Activity from another in android
另一个可能的问题是您的资源。您正在加载多个图像按钮,确保您的图像大小(字节)不会太大。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aa.sidd/com.example.aa.sidd.CustomSetting}: android.view.InflateException: Binary XML file line #17: Error inflating class
看到这个问题:
android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
第三,正如我在评论中提到的,我不认为将成员声明为静态作为解决方法是个好主意,请参阅此处 Avoiding memory leaks 静态成员、方法和 类 需要在计划任何应用程序时谨慎使用并考虑。用任何语言。嗯,这就是我的看法。
以DialogFragment 的形式显示新布局。这个想法是以 DialogFragment 弹出窗口的形式显示 increase/Decrease 选项。您可以从弹出窗口中 increase/decrease 大小。弹出窗口关闭后,所选大小将设置为 Activity 中的 TextView。
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static int updatedSize;
TextView greetingText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
greetingText = (TextView)findViewById(R.id.hello_world_text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
mycustomLayout();
return true;
}
return super.onOptionsItemSelected(item);
}
public void mycustomLayout(){
FragmentManager manager = getFragmentManager();
PopUp popUp = new PopUp(new FragmentDismissHandler());
popUp.show(manager,"POPUP");
}
public void refreshText(){
if( updatedSize>0)
greetingText.setTextSize(updatedSize);
}
private class FragmentDismissHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
refreshText();
}
}
}
PopUp.java(自定义布局为 DialogFragment)
public class PopUp extends DialogFragment implements View.OnClickListener{
ImageButton increase,decrease,set;
TextView sizeOfText;
static int size;
Handler handler;
public PopUp(Handler handler) {
this.handler = handler;
}
public PopUp(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
getDialog().setTitle("change text size");
View view = inflater.inflate(R.layout.my_custom_layout, container, false);
increase = (ImageButton)view.findViewById(R.id.inc);
increase.setOnClickListener(this);
decrease = (ImageButton)view.findViewById(R.id.dec);
decrease.setOnClickListener(this);
sizeOfText = (TextView)view.findViewById(R.id.size_text);
sizeOfText.setText("text");
set = (ImageButton)view.findViewById(R.id.ok);
set.setOnClickListener(this);
size = (int) sizeOfText.getTextSize();
return view;
}
@Override
public void onClick(View view) {
int id= view.getId();
if(id==R.id.inc){
size = size +1;
sizeOfText.setText("text");
sizeOfText.setTextSize(size);
}
if(id== R.id.dec){
size = size - 1;
sizeOfText.setText("text");
sizeOfText.setTextSize(size);
}
if(id== R.id.ok){
dismiss();
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
MainActivity.updatedSize = size;
handler.sendEmptyMessage(0);
}
}