无法启动另一个活动RuntimeException

时间:2018-01-12 00:16:24

标签: android

应用程序启动但由于RuntimeException而无法理解为什么,因此无法通过单击mainactivity中的按钮来启动另一个活动。其他活动以同样的方式开始。我认为我的xml文件是正确的,我在清单中有活动声明了我缺少的东西吗?

主要活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button buttonPaintings;
Button buttonTip;
Button buttonConvert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.cet325.assignment.R.layout.activity_main);
    buttonPaintings = (Button) findViewById(com.cet325.assignment.R.id.button_task);
    buttonPaintings.setOnClickListener(this);
    buttonTip = (Button) findViewById(com.cet325.assignment.R.id.button_tip);
    buttonTip.setOnClickListener(this);
    buttonConvert = (Button) findViewById(com.cet325.assignment.R.id.button_convert);
    buttonConvert.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(com.cet325.assignment.R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == com.cet325.assignment.R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {
    int id = view.getId();

    if (id == com.cet325.assignment.R.id.button_task) {
        Intent myIntent = new Intent(MainActivity.this,
                PaintingsActivity.class);
        startActivity(myIntent);
    }
    else if (id == com.cet325.assignment.R.id.button_tip) {
        Intent myIntent = new Intent(view.getContext(),
                TipActivity.class);
        startActivity(myIntent);
    }
    else if (id == com.cet325.assignment.R.id.button_convert) {
        Intent myIntent = new Intent(view.getContext(),
                MmCmActivity.class);
        startActivity(myIntent);
    }
}

}

activity_main.xml中:

<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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context="com.cet325.assignment.MainActivity"
            android:onClick="onClick">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Task List"
    android:id="@+id/button_task"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="72dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tip Calculator"
    android:id="@+id/button_tip"
    android:layout_below="@+id/button_task"
    android:layout_alignLeft="@+id/button_convert"
    android:layout_alignStart="@+id/button_convert"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Convert Mm Cm"
    android:id="@+id/button_convert"
    android:layout_below="@+id/button_tip"
    android:layout_centerHorizontal="true"/>

绘画活动:

public class PaintingsActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
    activity code
    }
}

堆栈跟踪说:

java.lang.ClassCastException:com.cet325.assignment.PaintingsActivity无法强制转换为android.widget.AdapterView $ OnItemSelectedListener

1 个答案:

答案 0 :(得分:1)

您需要implements AdapterView.OnItemSelectedListener上的PaintingsAvtivity