我有第一个活动,我开始另一个活动。
我创建了第二个:
app
并且我在class
文件夹和Java
activity_newActivity.xml
个文件
我在该活动中创建了一些内容,并且当我尝试从我的class
运行函数时,为什么函数无法正常工作,但我尝试在Log.d("Testing", "TEST")
函数中添加onCreate
并且我发现的是它甚至没有运行它。为什么会这样?
全班:
package rs.termodom.www.termodom;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;
import android.widget.TextView;
public class pocetna extends AppCompatActivity {
Display display;
Point screenSize;
TextView tp_txt;
ImageView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pocetna);
Log.d("PROBLEM", "PROBLEM");
//==========================================================================================
//==========================================================================================
//==========================================================================================
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
tp_txt = (TextView) findViewById(R.id.tp_txt);
tp_txt.setText("StA");
test = (ImageView)findViewById(R.id.akcija1_img);
test.getLayoutParams().height = 150;
test.getLayoutParams().width = screenSize.x;
//==========================================================================================
//==========================================================================================
//==========================================================================================
}
}
这是显而易见的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rs.termodom.www.termodom">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".pocetna"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
和XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rs.termodom.www.termodom.pocetna">
<android.support.constraint.ConstraintLayout
android:id="@+id/header"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/userName_txt"
android:layout_width="151dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="KORISNIK 123"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="1600 TP"
android:textColor="@android:color/holo_red_dark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/akcijaSlider"
android:layout_width="411dp"
android:layout_height="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="@+id/akcija1_img"
android:layout_width="268dp"
android:layout_height="112dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
我认为问题出在这里
display.getSize(screenSize);
screenSize
对象未初始化。
试试这个:
screenSize = new Point();
display = getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
答案 1 :(得分:0)
您是否在应用程序清单(AndroidManifest.xml)文件中注册了活动?
答案 2 :(得分:0)
问题是我没有正确地调用新活动。
我应该这样称呼它:
Intent myIntent = new Intent(getApplicationContext(), pocetna.class);
startActivity(myIntent);