我制作待办事项列表应用程序,当我尝试时,强行关闭。我检查了变量,监听器的所有内容,但仍未找到错误。我不知道我在这里错过了什么,至少对我来说,每一个薄的看起来都很完美。
我在这里添加了logcat
java代码:
public class MainActivity extends Activity {
public final static String STOCK_SYMBOL = "com.rashad.mytodolist.MEMO";
private SharedPreferences MemoEntered;
private TableLayout stockScrollView;
private EditText stockSymbolEditText;
Button enterStockSymbolButton;
Button deleteStocksButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MemoEntered = getSharedPreferences("stockList", MODE_PRIVATE);
stockScrollView = (TableLayout) findViewById(R.id.stockScrollView);
stockSymbolEditText = (EditText) findViewById(R.id.stockSymbolEditText);
enterStockSymbolButton = (Button) findViewById(R.id.enterStockSymbolButton);
deleteStocksButton = (Button) findViewById(R.id.deleteStocksButton);
enterStockSymbolButton.setOnClickListener(enterStockButtonListener);
deleteStocksButton.setOnClickListener(deleteStocksButtonListener);
updateSavedStockList(null);
}
private void updateSavedStockList(String newStockSymbol) {
String[] memo = MemoEntered.getAll().keySet().toArray(new String[0]);
Arrays.sort(memo, String.CASE_INSENSITIVE_ORDER);
if (newStockSymbol != null) {
insertStockInScrollView(newStockSymbol,
Arrays.binarySearch(memo, newStockSymbol));
} else {
for (int i = 0; i < memo.length; ++i) {
insertStockInScrollView(memo[i], i);
}
}
}
private void saveStockSymbol(String newStock) {
String isTheStockNew = MemoEntered.getString(newStock, null);
SharedPreferences.Editor preferencesEditor = MemoEntered.edit();
preferencesEditor.putString(newStock, newStock);
preferencesEditor.apply();
if (isTheStockNew == null) {
updateSavedStockList(newStock);
}
}
private void insertStockInScrollView(String stock, int arrayIndex) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newStockRow = inflater.inflate(R.layout.memorow, null);
TextView newStockTextView = (TextView) newStockRow
.findViewById(R.id.stockSymbolTextView);
newStockTextView.setText(stock);
stockScrollView.addView(newStockRow, arrayIndex);
}
public OnClickListener enterStockButtonListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
if (stockSymbolEditText.getText().length() > 0) {
saveStockSymbol(stockSymbolEditText.getText().toString());
stockSymbolEditText.setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
stockSymbolEditText.getWindowToken(), 0);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle(R.string.Nothing);
builder.setPositiveButton(R.string.ok, null);
builder.setMessage(R.string.Enter_memo);
AlertDialog theAlertDialog = builder.create();
theAlertDialog.show();
}
}
};
public OnClickListener deleteStocksButtonListener = new OnClickListener() {
public void onClick(View v) {
stockScrollView.removeAllViews();
SharedPreferences.Editor preferencesEditor = MemoEntered.edit();
preferencesEditor.clear();
preferencesEditor.apply();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menu.add(0, 1, 0, "about My ToDo");
menu.add(0, 2, 1, "Quit");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent myIntent = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(myIntent);
;
return true;
case 2:
finish();
return true;
}
return false;
}
}
xml文件:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#000000" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/stockSymbolEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:layout_weight="1">
<requestFocus />
</EditText>
<Button
android:id="@+id/enterStockSymbolButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#006699" >
<TextView
android:id="@+id/stockSymbolTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="@string/List"
android:textColor="#000000"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#006699"
android:layout_weight="1" >
<ScrollView
android:id="@+id/stockScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="2"
android:padding="5dp" >
<TableLayout
android:id="@+id/tablememo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="yes" >
</TableLayout>
</ScrollView>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/deleteStocksButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:layout_weight="1"
android:text="@string/Button1" />
</TableRow>
</TableLayout>
错误的logcat:
02-20 12:03:47.119: E/AndroidRuntime(2872): FATAL EXCEPTION: main
02-20 12:03:47.119: E/AndroidRuntime(2872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rashad.mytodolist/com.rashad.mytodolist.MainActivity}: java.lang.ClassCastException: android.widget.ScrollView
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.os.Looper.loop(Looper.java:130)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread.main(ActivityThread.java:3835)
02-20 12:03:47.119: E/AndroidRuntime(2872): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 12:03:47.119: E/AndroidRuntime(2872): at java.lang.reflect.Method.invoke(Method.java:507)
02-20 12:03:47.119: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
02-20 12:03:47.119: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
02-20 12:03:47.119: E/AndroidRuntime(2872): at dalvik.system.NativeStart.main(Native Method)
02-20 12:03:47.119: E/AndroidRuntime(2872): Caused by: java.lang.ClassCastException: android.widget.ScrollView
02-20 12:03:47.119: E/AndroidRuntime(2872): at com.rashad.mytodolist.MainActivity.onCreate(MainActivity.java:44)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-20 12:03:47.119: E/AndroidRuntime(2872): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
02-20 12:03:47.119: E/AndroidRuntime(2872): ... 11 more
答案 0 :(得分:2)
<ScrollView
android:id="@+id/stockScrollView"
...
stockScrollView = (TableLayout) findViewById(R.id.stockScrollView);
您无法将ScrollView
投射到TableLayout
。因此,logcat中的java.lang.ClassCastException: android.widget.ScrollView
。
要解决此问题,请更改
private TableLayout stockScrollView;
到
private ScrollView stockScrollView;
和
stockScrollView = (TableLayout) findViewById(R.id.stockScrollView);
到
stockScrollView = (ScrollView) findViewById(R.id.stockScrollView);
可能存在其他问题,但这至少是向前迈出的一步。
通常,您需要学习自己调试程序:当出现异常时,从logcat中的堆栈跟踪中查找原因和位置。理论化异常的解释。通过修复理论化问题并再次运行代码来测试您的理论。根据需要重复。