I have two layouts, one has editText and the other has a listview to change the editText's Fonts.
The app is getting crushed at clicking the button for opening the layout with listView probably because of the array Adapters.
Please help me.
Every Button is working fine. Just the btnChangeFont button is problematic.
My Code:
MainActivity.java[enter image description here][1]
package com.example.xarn.test;
public class MainActivity extends ActionBarActivity {
String[] values = new String[]{
"art_science",
"big_bold",
"bold_itallic",
"crazy_days",
"gabriola",
"handwritting",
"new_bold",
"new_itallic",
"oldschool",
"rough&tough",
"sketch",
};
ListView listView;
EditText text;
LinearLayout choosefonts;
ImageButton btnOpen, btnClose, btnNew, btnSave, btnDelete,btnChangeFont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.text);
listView =(ListView)findViewById(R.id.listView);
btnChangeFont = (ImageButton)findViewById(R.id.btnChangeFont);
btnChangeFont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.choosefonts);
choosefonts = (LinearLayout)findViewById(R.id.choosefonts);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
break;
case 1: text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/big_bold.ttf"));
break;
case 3:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/bold_itallic.ttf"));
break;
case 4:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/crazy_days.ttf"));
break;
case 5:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/gabriola.ttf"));
break;
case 6:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/handwritting.ttf"));
break;
case 7:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_bold.ttf"));
break;
case 8:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_itallic.ttf"));
break;
case 9:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/oldschool.ttf"));
break;
case 10:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/rough&tough.ttf"));
break;
case 11:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sketch.ttf"));
break;}
}
});
}
});
text.setHint("Write Your Note Here............");
text.setTextIsSelectable(true);
btnSave = (ImageButton)findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Save As");
d.setContentView(R.layout.dialogsave);
d.show();
Button btnDialogSave = (Button)d.findViewById(R.id.btnDialogSave);
btnDialogSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText saveText = (EditText)d.findViewById(R.id.SaveText);
try {
File myFile = new File("/sdcard/"+saveText.getText()+".txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(text.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
""+saveText.getText()+" Saved",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
d.dismiss();
}
});
}
});
btnNew = (ImageButton)findViewById(R.id.btnNew);
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("");
}
});
btnClose = (ImageButton)findViewById(R.id.btnClose);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btnOpen = (ImageButton)findViewById(R.id.btnOpen);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d2 = new Dialog(MainActivity.this);
d2.setTitle("Open");
d2.setContentView(R.layout.dialogopen);
d2.show();
Button btnDialogOpen = (Button)d2.findViewById(R.id.btnDialogOpen);
btnDialogOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText openText = (EditText)d2.findViewById(R.id.OpenText);
try {
File myFile = new File("/sdcard/"+openText.getText()+".txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
text.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
""+openText.getText()+" Opened",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
d2.dismiss();
}
});
}
});
}
@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) {
// 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if(id == R.id.changeFont){
final Dialog d3 = new Dialog(MainActivity.this);
d3.setContentView(R.layout.choosefonts);
d3.setTitle("Choose Fonts");
d3.show();
/* String[] values = new String[]{
"art_science",
"big_bold",
"bold_itallic",
"crazy_days",
"gabriola",
"handwritting",
"new_bold",
"new_itallic",
"oldschool",
"rough&tough",
"sketch",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){case 0:text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
// text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
break;
case 1: text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/big_bold.ttf"));
break;
case 3:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/bold_itallic.ttf"));
break;
case 4:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/crazy_days.ttf"));
break;
case 5:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/gabriola.ttf"));
break;
case 6:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/handwritting.ttf"));
break;
case 7:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_bold.ttf"));
break;
case 8:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_itallic.ttf"));
break;
case 9:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/oldschool.ttf"));
break;
case 10:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/rough&tough.ttf"));
break;
case 11:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sketch.ttf"));
break;}
}
});*/
}
return super.onOptionsItemSelected(item);
}
}
xml code for layout 1:
<RelativeLayout
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="200pt"
android:gravity="top"
android:scrollbars="horizontal"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnNew"
android:layout_below="@+id/text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/new1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:layout_alignBottom="@+id/btnNew"
android:layout_toRightOf="@+id/btnNew"
android:layout_toEndOf="@+id/btnNew"
android:background="@drawable/save"
android:clickable="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnOpen"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/btnSave"
android:layout_toEndOf="@+id/btnSave"
android:background="@drawable/open4"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnClose"
android:background="@drawable/close"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/btnOpen"
android:layout_toEndOf="@+id/btnOpen" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:background="@drawable/delete"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/btnClose"
android:layout_toEndOf="@+id/btnClose" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnChangeFont"
android:layout_alignBottom="@+id/btnDelete"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/btnDelete"
android:background="@drawable/star"/>
<RelativeLayout/>
logcat error
06-22 11:48:18.188 8058-8058/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.xarn.test, PID: 8058
java.lang.NullPointerException
at com.example.xarn.test.MainActivity$1.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:4443)
at android.view.View$PerformClick.run(View.java:18442)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
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:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)