我正在我的体育馆里参加 C 编程考试。每次我单击“krypter”或“dekrypter”(它们是两个按钮)时,我的应用程序都会崩溃。我在代码中的一些不同位置收到上述错误,无法弄清楚问题所在。我已经看过许多其他类似的问题,但没有一个让我看到问题所在。这两个活动大部分是相同的,并且对于 XML 是相同的。
您可以从我的主要活动中打开的一项活动的代码
public class Krypteringstype extends Activity {
public Button slider;
public Button krypterKnap;
public EditText tekst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.krypteringstype); // viser krypteringssiden
slider = (Button) findViewById(R.id.handle); // forbinder xml-knappen og knappen "slider", som er defineret tidligere
slider.getBackground().setAlpha(230); // gør baggrunden, som jeg bruger let transparent
//tekst = (EditText) findViewById(editText1);
krypterKnap = (Button) findViewById(R.id.krypter);
krypterKnap.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
krypterKnap();
}
});
}
private void krypterKnap() {
if (tekst.getText().equals("")){
Toast.makeText(getApplicationContext(), "Skriv tekst først", Toast.LENGTH_LONG).show();
}
else {
konverter();
}
}
private void konverter(){
String initialString = "yourString";
char[] chars = initialString.toCharArray();
for (int i = 0; i < chars.length; ++i)
chars[i] = (char)((int)chars[i] + 2);
// String resultString = new String(chars);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.krypter, menu);
return true;
}
}
上述活动的 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/blue_binary_code_1600_1200"
tools:context=".Krypter" >
<Button
android:id="@+id/krypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="30dp"
android:background="@drawable/background_large"
android:gravity="center"
android:minHeight="35dp"
android:minWidth="100dp"
android:text="@string/valg"
android:textSize="20sp" />
<Button
android:id="@+id/standardKrypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="30dp"
android:background="@drawable/background_large"
android:gravity="center"
android:minHeight="35dp"
android:minWidth="100dp"
android:text="@string/standard"
android:textSize="20sp" />
<Button
android:id="@+id/overskriftKrypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:minWidth="250dp"
android:text="@string/overskrift"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/krypter"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/overskriftKrypter"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" >
<SlidingDrawer
android:id="@+id/slidingDrawerKrypter"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/ceasar" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/ceasarNr"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
</SlidingDrawer>
<SlidingDrawer
android:id="@+id/slidingDrawer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/abe" />
<LinearLayout
android:id="@+id/content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background"
android:orientation="vertical" >
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/a"
android:inputType="number" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/b"
android:inputType="number" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/e"
android:inputType="number" />
</LinearLayout>
</SlidingDrawer>
<SlidingDrawer
android:id="@+id/slidingDrawer3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/rsa" />
<LinearLayout
android:id="@+id/content3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background"
android:orientation="vertical" >
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prim1"
android:inputType="number" />
<EditText
android:id="@+id/editText7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prim2"
android:inputType="number" />
<EditText
android:id="@+id/editText6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/k"
android:inputType="number" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>
您可以从我的主要活动中打开的第二个活动的代码
public class Dekrypteringstype extends Activity {
public Button slider;
public Button krypterKnap;
public EditText tekst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dekrypteringstype); // viser krypteringssiden
slider = (Button) findViewById(R.id.handle); // forbinder xml-knappen og knappen "slider", som er defineret tidligere
slider.getBackground().setAlpha(230); // gør baggrunden, som jeg bruger let transparent
//tekst = (EditText) findViewById(editText1);
krypterKnap = (Button) findViewById(R.id.dekrypter);
krypterKnap.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
krypterKnap();
}
});
}
private void krypterKnap() {
if (tekst.getText().equals("")){
Toast.makeText(getApplicationContext(), "Skriv tekst først", Toast.LENGTH_LONG).show();
}
else {
konverter();
}
}
private void konverter(){
String initialString = "yourString";
char[] chars = initialString.toCharArray();
for (int i = 0; i < chars.length; ++i)
chars[i] = (char)((int)chars[i] + 2);
// String resultString = new String(chars);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dekrypter, menu);
return true;
}
}
上述活动的 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/blue_binary_code_1600_1200"
tools:context=".Dekrypter" >
<Button
android:id="@+id/dekrypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="30dp"
android:background="@drawable/background_large"
android:gravity="center"
android:minHeight="35dp"
android:minWidth="100dp"
android:text="@string/valg"
android:textSize="20sp" />
<Button
android:id="@+id/standardDekrypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="30dp"
android:background="@drawable/background_large"
android:gravity="center"
android:minHeight="35dp"
android:minWidth="100dp"
android:text="@string/standard"
android:textSize="20sp" />
<Button
android:id="@+id/overskriftDekrypter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:minWidth="250dp"
android:text="@string/overskrift2"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/dekrypter"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/overskriftDekrypter"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" >
<SlidingDrawer
android:id="@+id/slidingDrawerDekrypter"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/ceasar" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/ceasarNr"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
</SlidingDrawer>
<SlidingDrawer
android:id="@+id/slidingDrawer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/abe" />
<LinearLayout
android:id="@+id/content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background"
android:orientation="vertical" >
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/a"
android:inputType="number" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/b"
android:inputType="number" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/e"
android:inputType="number" />
</LinearLayout>
</SlidingDrawer>
<SlidingDrawer
android:id="@+id/slidingDrawer3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_large"
android:minHeight="35dp"
android:text="@string/rsa" />
<LinearLayout
android:id="@+id/content3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button_background"
android:orientation="vertical" >
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prim1"
android:inputType="number" />
<EditText
android:id="@+id/editText7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prim2"
android:inputType="number" />
<EditText
android:id="@+id/editText6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/k"
android:inputType="number" />
</LinearLayout>
</SlidingDrawer>
</LinearLayout>