更改edittext字体大小时出现异常

时间:2014-12-07 22:17:19

标签: android exception android-edittext font-size

这段代码构成了一个例外 我不确切地知道问题出在哪里..

  Spinner dropdown = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
    dropdown.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

            etFName.setTextSize(TypedValue.COMPLEX_UNIT_SP,Integer.parseInt(items[position]));                    

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

这是我的logcat:

 12-07 23:04:35.047: E/AndroidRuntime(7505): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.invitationcard/com.example.invitationcard.Graduation}: java.lang.NullPointerException
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.os.Handler.dispatchMessage(Handler.java:99)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.os.Looper.loop(Looper.java:137)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread.main(ActivityThread.java:4424)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at java.lang.reflect.Method.invokeNative(Native Method)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at java.lang.reflect.Method.invoke(Method.java:511)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at dalvik.system.NativeStart.main(Native Method)
 12-07 23:04:35.047: E/AndroidRuntime(7505): Caused by: java.lang.NullPointerException
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at com.example.invitationcard.Graduation.onCreate(Graduation.java:46)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.Activity.performCreate(Activity.java:4492)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 12-07 23:04:35.047: E/AndroidRuntime(7505):    ... 11 more

有谁知道我的错误是什么?

2 个答案:

答案 0 :(得分:0)

我注意到的第一件事是edittext

etFName.setTextSize(TypedValue.COMPLEX_UNIT_SP,Integer.parseInt(items[position])); 

您正在将对象解析为int。你有一个名为textSize的项目内的变量吗?

etFName.setTextSize(TypedValue.COMPLEX_UNIT_SP,Integer.parseInt(items[position].textSize)); 

其次,您收到的错误与您提供的代码无关。 您正尝试传递某些内容,创建新活动或在某处创建一个null的视图。

如果您想要更深入的解释,则必须提供更多代码。

新信息:

问题在于:

Intent intent = new Intent(context, g1.class);
                        startActivity(intent);
      Intent intent1 = new Intent(context , g1.class); 
      intent1.putExtra("fname" , et.getText().toString());
      startActivity(intent1);

这没有任何意义。您正在创建2个意图并启动它们。你为什么要那样做? 当第一个活动被打开时,它将失去该背景。

卸下:

Intent intent = new Intent(context, g1.class);
                        startActivity(intent);

请确保使用IDE提供的正确调试工具。那个bug应该很容易追踪。

答案 1 :(得分:0)

这是我的.java

public class Graduation extends ActionBarActivity {

ImageView imageView ;
private Spinner spinner;
public static final String[]items = {"8", "12", "16", "18", "20", "24", "28"};

 EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graduation);

    Spinner dropdown = (Spinner)findViewById(R.id.spinner);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
    dropdown.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

            et.setTextSize(TypedValue.COMPLEX_UNIT_SP,Integer.parseInt(items[position]));                    

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

    addListenerOnImageg1() ;

    et = (EditText) findViewById(R.id.et);

}



public void addListenerOnImageg1() {

    final Context context = this;

    imageView = (ImageView) findViewById(R.id.g1);

    imageView.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, g1.class);
                        startActivity(intent);
      Intent intent1 = new Intent(context , g1.class); 
      intent1.putExtra("fname" , et.getText().toString());
      startActivity(intent1);


        }

    }); }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是我的xml文件:

 <?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

 <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backf"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter your Text Here  : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="choose your frame : "
    android:textAppearance="?android:attr/textAppearanceLarge" />


<ImageView
    android:id="@+id/g1"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g1" />
 <ImageView
    android:id="@+id/g2"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g2" />
  <ImageView
    android:id="@+id/g3"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g3" />
   <ImageView
    android:id="@+id/g4"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g4" />
    <ImageView
    android:id="@+id/g5"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g5" />
    <ImageView
    android:id="@+id/g6"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g6" />
    <ImageView
    android:id="@+id/g7"
    android:layout_width="122dp"
    android:layout_height="111dp"
    android:clickable="true"
    android:onClick="onClick"
    android:src="@drawable/g7" />

 </LinearLayout>
 </ScrollView>

当我把微调器代码作为注释时代码正常工作,所以异常是在微调器代码中