2014-12-07 38 views
0

當這個代碼段破例 我不知道exxactly問題出在哪裏..異常改變的EditText字號

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 

有任何人知道什麼是我的錯誤?

+0

請發佈完整活動的代碼 – JacksOnF1re 2014-12-07 22:20:10

+0

Whi ch行引起異常? – Rami 2014-12-07 22:23:38

+0

好吧,我會把它作爲一個答案 – 2014-12-07 22:41:28

回答

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)); 

其次,您收到的錯誤與您提供的代碼無關。 你正在嘗試傳遞一些東西,創建一個新的活動或創建一個空視圖。

如果您想要更深入的解釋,您將不得不提供更多的代碼。

新的信息:

問題就在這裏:

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提供適當的調試工具。該錯誤應該很容易追蹤。

+0

檢查出我的答案,請:) :) – 2014-12-07 22:46:43

+0

是的,我會... ... thaaank你@victor :)))) – 2014-12-08 01:03:32

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> 

當我把微調代碼註釋的代碼工作正常,,所以Spinner代碼中的例外情況