Android:app在发布活动后崩溃

时间:2016-06-15 10:20:44

标签: java android listview random textview

我有这个应用程序,但由于某种原因,我不能让它通过启动屏幕工作。用户单击列表视图上的项目后,它会在活动中生成一个随机int,并根据该项更改textView。如果用户的输入与正确的.contains字符串匹配,它将打印出一个吐司。 这是发布活动:     包com.eoincoogan.owner.omegaphi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
 import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LaunchActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launch);

    ListView lv = (ListView) findViewById(R.id.subjectListView);

    String[] subjectArray = new String[]{"Physics","Chemistry","Economics", "Geography"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjectArray);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                    if (position == 0) {
                        Intent intent = new Intent(LaunchActivity.this, PhysicsFragment.class);
                        startActivity(intent);
                    } else if (position == 1) {
                        Intent intent = new Intent(LaunchActivity.this, ChemFragment.class);
                        startActivity(intent);
                    } else if (position == 2) {
                        Intent intent = new Intent(LaunchActivity.this, EconFragment.class);
                        startActivity(intent);
                    } else if (position == 3) {
                        Intent intent = new Intent(LaunchActivity.this, GeoActivity.class);
                        startActivity(intent);
                    }

                }
            }


    );

}


}

这是LaunchActivity之后的活动:     包com.eoincoogan.owner.omegaphi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;


public class PhysicsFragment extends AppCompatActivity {

private TextView definitionTextView = (TextView) findViewById(R.id.definitonTextView);
String definitionText = definitionTextView.getText().toString();


private EditText userInputText = (EditText) findViewById(R.id.userInputText);
String userInput = userInputText.getText().toString();

Random rand = new Random();
int value = rand.nextInt(3);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_physics_fragment);

    Button button = (Button)findViewById(R.id.button);
    Button buttonTwo = (Button)findViewById(R.id.button2);

    final Toast firstToast = Toast.makeText(this, "Correct", Toast.LENGTH_LONG);
    final Toast secondToast = Toast.makeText(this, "Correct", Toast.LENGTH_LONG);


    button.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    switch (value) {
                        case 0:
                            definitionText = "Diode";

                            if (userInput.contains("Current" + "one way" + "allows")) {
                                firstToast.show();
                            } else {
                                secondToast.show();
                            }

                        case 1:
                            definitionText = "First law of refraction";

                            if (userInput.contains("insidence" + "normal" + "refracted" + "ray")) {
                                firstToast.show();
                            } else {
                                secondToast.show();
                            }

                        case 2:
                            definitionText = "Second law of refraction";

                            if (userInput.contains("angle" + "incidence" + "reflection" + "equal")) {
                                firstToast.show();
                            } else {
                                secondToast.show();
                            }

                    }

                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            }
    );

    //button to launch screen//

    buttonTwo.setOnClickListener(
            new Button.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent myIntent = new Intent(PhysicsFragment.this, LaunchActivity.class);
                    startActivity(myIntent);

                }
            }
    );
}





@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_driver, 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;
    }

    return super.onOptionsItemSelected(item);
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

将LaunchActivity添加到Manifest文件

<activity android:name=".LaunchActivity"></activity>

java文件,

Intent myIntent = new Intent(context, LaunchActivity.class);
  startActivity(myIntent);