我有两个布局,一个有editText,另一个有listview来改变editText的字体。
点击按钮打开布局时,应用程序因为数组适配器而受到压制。
请帮帮我。
每个按钮都正常工作。只是btnChangeFont按钮有问题。
我的代码:
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);
}
}
布局1的xml代码:
<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错误
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)
答案 0 :(得分:0)
我相信您忘记将名为“text”的EditText视图分配给xml布局中的匹配视图。
答案 1 :(得分:0)
定义变量text
,但永远不会为其赋值。